Find a string

J

Jenn Civello

Is there a way to Find a string of words while searching all tables and
queries etc., without going into each them individually?
 
A

Andy Couch - UK Access User Group

One suggetion is to use some program code to trawl through the tables, and
then the individual fields in each table. The difficulty is that because the
tables will presumably have different structures, recording where you found a
match could be quite difficult. You would need to save the matched table name
and record's primary key.

The following code is not going to be very efficient, but should point the
way to one method through which you could achieve this Jenn.


Sub FindTheString(TheString As String)
Dim db As dao.Database
Set db = CurrentDb
Dim fld As dao.Field
Dim rst As dao.Recordset
Dim td As dao.TableDef
Dim sqlstr As String

For Each td In db.TableDefs
If UCase(Left(td.Name, 4)) <> "MSYS" Then
For Each fld In td.Fields
If fld.Type = dbText Then
sqlstr = "SELECT * FROM " & td.Name & " WHERE [" & fld.Name
& "] like '*" & TheString & "*'"
Set rst = db.OpenRecordset(sqlstr)
If Not rst.EOF Then
Debug.Print rst(0)
' here you would need to record the match
End If
rst.Close
End If
Next
End If
Next
Set rst = Nothing
db.Close
Set db = Nothing
End Sub

You would also need to take care of any embedded quotes in the search string.
 
Top