CodeItBetter Programming Another VB Programming Blog

How to Use ADO to list the tables and fields in a database

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'Database - How to Use ADO to list the tables and fields in a database
'Use the Connection object's OpenSchema method to get a list of tables.

' List the tables in the database.
Private Sub ListTables(ByVal db_name As String)
    Dim statement As String
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
 
    ' Open a connection.
    Set conn = New ADODB.Connection
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & db_name & _
        ";" & "Persist Security Info=False"
    conn.Open
 
    lstTables.Clear
    lstFields.Clear
 
    ' Use OpenSchema and get the table names.
    ' The final argument in the parameter array
    ' is "Table" to indicate we want a list of tables.
    Set rs = conn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "Table"))
    Do While Not rs.EOF
        lstTables.AddItem rs!table_name
        rs.MoveNext
    Loop
 
    rs.Close
    conn.Close
End Sub
 
'When the user clicks on a table, use OpenSchema again to get a list of table 
'selected table's fields.

' List the fields in this table.
Private Sub ListFields(ByVal db_file As String, ByVal db_table_name As String)
    Dim statement As String
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
 
    ' Open a connection.
    Set conn = New ADODB.Connection
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & db_file & _
        ";" & "Persist Security Info=False"
    conn.Open
 
    lstFields.Clear
 
    ' Use OpenSchema and get the table names.
    ' The final argument in the parameter array
    ' gives the table's name.
    Set rs = conn.OpenSchema(adSchemaColumns, Array(Empty, Empty, db_table_name))
 
    Do While Not rs.EOF
        lstFields.AddItem rs!COLUMN_NAME
        rs.MoveNext
    Loop
 
    rs.Close
    conn.Close
End Sub
Filed under: Database Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.