• looping in table field and return the name

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » looping in table field and return the name

    Author
    Topic
    #463789

    I use this conn:

    cn.ConnectionString = “Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=” & DATA_BASE & “;Data Source=xxxxxxx”
    cn.Open

    and i know the name of table of DATA_BASE similar MyTable

    How to retive the name of fileds in Table MyTable????

    Viewing 1 reply thread
    Author
    Replies
    • #1185113

      You can open a recordset rst on MyTable, then use a loop like this:

      Code:
      Dim fld As ADODB.Field
      Dim strName As String
      For Each fld In rst.Fields
        strName = fld.Name
        ...
        ...
      Next fld
      • #1186880

        You can open a recordset rst on MyTable, then use a loop like this:

        Code:
        Dim fld As ADODB.Field
        Dim strName As String
        For Each fld In rst.Fields
          strName = fld.Name
          ...
          ...
        Next fld

        Hi Hans…
        Based the code is possible to write into a text file similar c:myfile.txt the name of fileds separated from “;”?
        I want to use to make the header of CSV text file.

        • #1186886

          Yes. Use code like this:

          Code:
          ...
          Dim fld As ADODB.Field
          Dim strName As String
          Dim strLine As String
          Dim f As Integer
          
          ' Create text file
          f = FreeFile
          Open "C:MyFile.txt" For Output As #f
          
          For Each fld In rst.Fields
            ' Get name of field
            strName = fld.Name
            ' Append to strLine
            strLine = strLine & ";" & strName
          Next fld
          
          ' Write strLine to file, omitting the first semicolon ;
          Print #f, Mid(strLine, 2)
          
          ' Close text file
          Close #f
          ...
          • #1186888

            Yes. Use code like this:

            Code:
            ...
            Dim fld As ADODB.Field
            Dim strName As String
            Dim strLine As String
            Dim f As Integer
            
            ' Create text file
            f = FreeFile
            Open "C:MyFile.txt" For Output As #f
            
            For Each fld In rst.Fields
              ' Get name of field
              strName = fld.Name
              ' Append to strLine
              strLine = strLine & ";" & strName
            Next fld
            
            ' Write strLine to file, omitting the first semicolon ;
            Print #f, Mid(strLine, 2)
            
            ' Close text file
            Close #f
            ...

            wow!

            But the next prob is:
            i want to write (after this header) also the value of recordset separeted from “;” how to? i not have any idea.
            In effet i wat to loop into recorset and create the rest of CSV

            • #1186890

              You can export from Access to a .csv file.
              You can use CopyFromRecordset to import from Access into Excel; you can save the result as a .csv file.
              So why should we write code for you for something that can already be done?

      • #1186960

        You can open a recordset rst on MyTable, then use a loop like this:

        Code:
        Dim fld As ADODB.Field
        Dim strName As String
        For Each fld In rst.Fields
          strName = fld.Name
          ...
          ...
        Next fld

        hi… is possible during this loop to know also the property of each filed?
        Similar is is numeric, double text ecc….

    • #1185230

      I think the method Hans describes is fastest, particularly because you have the table open already. If it doesn’t work, you can try the ADOX library: ADO Extensions for Data Definition Language and Security (ADOX). I personally find ADOX hard to use, but my database work is quite limited, so that could explain it.

    Viewing 1 reply thread
    Reply To: looping in table field and return the name

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: