Results 1 to 3 of 3

Thread: How can I...

  1. #1

    Thread Starter
    Addicted Member AngelinaM's Avatar
    Join Date
    Jan 2001
    Location
    Tampa, Florida
    Posts
    175

    Question

    get all of the field names from a table in a SQL database and write them out to a VB form?

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    AngelinaM,

    The following example lists the fields for a recordSet created from an Access database, but you should be able to apply it to a SQL database too. I just threw this code together, so minor changes may be needed to get all the fields. The most important point is that you use the ADODB.fields object to retrieve the field names.

    Code:
    Option Explicit
    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim adofields As ADODB.Fields
    
    Private Sub Command1_Click()
        Dim x As Integer
        Dim adofields As ADODB.Fields
        
        Set adofields = rs.Fields
        For x = 1 To (adofields.Count - 1)
            Debug.Print adofields.Item(x).Name
        Next x
    End Sub
    
    Private Sub Form_Load()
        Set cnn = New ADODB.Connection
        Set rs = New ADODB.Recordset
        
        cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb;" & _
                "Persist Security Info=False"
        rs.Open "Select * from Customers", cnn, adOpenKeyset, adLockOptimistic
    End Sub

  3. #3

    Thread Starter
    Addicted Member AngelinaM's Avatar
    Join Date
    Jan 2001
    Location
    Tampa, Florida
    Posts
    175

    Cool PERFECT!

    THANK YOU!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width