|
|
Title | Use ADOX to search the table columns in an Access database for a string in Visual Basic 6 |
Description | This example shows how to use ADOX to search the table columns in an Access database for a string in Visual Basic 6. |
Keywords | ADOX, Access, database, table, column, table columns, search columns, Visual Basic 6 |
Categories | Database |
|
|
First add references to the libraries:
- Microsoft ActiveX Data Objects 2.6 Library
- Microsoft ADO Ext. 2.6 for DLL and Security
(Or whatever your versions are.)
When you click the Search Tables button, the following code executes. It opens the database and uses the ADOX catalog to learn about the database. It loops through the Tables collection and looks for objects that have the TABLE type. (You could remove this test to make the code search queries and system tables in addition to normal tables.) The code searches TABLE type objects for the target string. If it finds the string, it lists the table's name and its columns in a ListView control.
|
|
Private Sub cmdSearchTables_Click()
Dim conn As ADODB.Connection
Dim cat As ADOX.Catalog
Dim i As Integer
Dim target As String
Dim cols As String
Dim col As ADOX.Column
Dim list_item As ListItem
MousePointer = vbHourglass
lvwTables.ListItems.Clear
DoEvents
' Open the connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source=" & txtDatabase.Text
conn.Open
' Make a catalog for the database.
Set cat = New ADOX.Catalog
Set cat.ActiveConnection = conn
' Search the catalog's tables.
target = LCase$(txtSearchFor.Text)
For i = 0 To cat.Tables.Count - 1
If cat.Tables(i).Type = "TABLE" Then
cols = ""
For Each col In cat.Tables(i).Columns
cols = cols & ", " & col.Name
Next col
If InStr(LCase$(cols), target) > 0 Then
Set list_item = _
lvwTables.ListItems.Add(Text:=cat.Tables(i).Name)
cols = Mid$(cols, 3)
list_item.SubItems(1) = cols
End If
End If
Next i
conn.Close
MousePointer = vbDefault
If lvwTables.ListItems.Count = 0 Then MsgBox "Done"
End Sub
|
|
|
|
|
|