Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: RE: How to retrieve field names from a table


Message #1 by Rick Crofoot <rcrofoot@e...> on Mon, 15 Apr 2002 22:20:21 +0100
Hi-
Try this code...It omits all system tables from the list, and populates a combobox...You could easily alter this to omit VIEWs:

Private Sub Form_Load()

    'instantiate connection & recordset object variables
    Set cn = New ADODB.Connection
    Set rsSchema = New ADODB.Recordset
    
    'load connectionstring data
    cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;" & _
                                         "data source=d:\biblio.mdb"

    'open the connection to database
    cn.Open
    
    'return and assign a recordset object containing table related metadata
    Set rsSchema = cn.OpenSchema(adSchemaTables)
    
    'populate combobox using column 3 (table name) of returned recordset
    Do Until rsSchema.EOF
        'add this table name only if it a user table (i.e. does not contain "MSys")
        If InStr(rsSchema.Fields(2).Value, "MSys") = 0 Then
            cboSchema.AddItem rsSchema.Fields(2).Value
        End If
        'move to the next record
        rsSchema.MoveNext
    Loop
    
    'close the recordset
    rsSchema.Close
    'destroy the recordset object
    Set rsSchema = Nothing

End Sub

Good luck...
Rick



-----Original Message-----
From:	Raju S.Pusapati [SMTP:rajupusapati@y...]
Sent:	Monday, April 15, 2002 11:12 PM
To:	professional vb
Subject:	[pro_vb] How to retrive field names from a table

Hi Everybody,
I try to retrive the field values of a selected table through ADO using 
the following code.I am getting run time error:3265 and the filds list is 
not displaying.Please let me know how to get the field names from the 
table.
*************
Dim cnn As New ADODB.Connection
   Dim cat As New ADOX.Catalog
   Dim tbl As ADOX.Table
   Dim i As Integer
   cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
     "Data Source=C:\linker.mdb;"
   
   Dim rs As ADODB.Recordset
   Set rs = cnn.OpenSchema(adSchemaColumns)
   
    'Loop through the results and print
    'the names in the debug window
    Do Until rs.EOF
    If rs.Fields("TABLE_COLUMNS") <> "VIEW" Then
        Debug.Print rs.Fields("TABLE_COLUMNS")
    End If
    rs.MoveNext
*************
Thanks.
    Loop


  Return to Index