Title | Use DAO to search for a string in database table and field names |
Description | This example shows how to use DAO to search for a string in database table and field names in Visual Basic 6. |
Keywords | database, search, table, field, DAO |
Categories | Database |
|
|
To search the database's table names, the program loops through the database's TableDefs collection. It uses the Like operator to compare the table names to a search string.
|
|
Private Sub cmdSearchTables_Click()
Dim results As String
Dim like_expression As String
Dim db As DAO.Database
Dim table_def As DAO.TableDef
txtResults.Text = ""
Screen.MousePointer = vbHourglass
DoEvents
like_expression = LCase$(txtFieldLike.Text)
Set db = DBEngine(0).OpenDatabase(txtDatabase.Text)
For Each table_def In db.TableDefs
If LCase$(table_def.Name) Like like_expression Then
results = results & table_def.Name & vbCrLf
End If
Next table_def
txtResults.Text = results
Screen.MousePointer = vbDefault
End Sub
|
|
To search the database's field names, the program loops through the database's TableDefs collection. For each table, it loops through the table's Fields collection. It uses the Like operator to compare the field names to a search string.
|
|
Private Sub cmdSearchFields_Click()
Dim results As String
Dim like_expression As String
Dim db As DAO.Database
Dim table_def As DAO.TableDef
Dim field_def As DAO.Field
txtResults.Text = ""
Screen.MousePointer = vbHourglass
DoEvents
like_expression = LCase$(txtFieldLike.Text)
Set db = DBEngine(0).OpenDatabase(txtDatabase.Text)
For Each table_def In db.TableDefs
For Each field_def In table_def.Fields
If LCase$(field_def.Name) Like like_expression _
Then
results = results & table_def.Name & _
"." & field_def.Name & vbCrLf
End If
Next field_def
Next table_def
txtResults.Text = results
Screen.MousePointer = vbDefault
End Sub
|
|
|
|