Your code never "filters" your xyzFields table based on the fragment that
you are seeking. If you look at the code that I'd posted, I use the string
fragment to obtain a recordset that contains the records where the FieldName
contains that fragment, and then I loop through that recordset to read its
records.
You said that you tried my code and it always said "no such field"...did you
try the code exactly as posted? or did you change it? If you changed it, how
did you change it?
Your code might be better this way (I am assuming that the field names in
xyzResults table are the same as in xyzFields table):
Function myfun()
On Error Resume Next
Dim db As DAO.Database
Dim rsXYZFields As DAO.Recordset
Dim mTable As String
Dim mField As String
Dim strSQL As String
Dim strFIND As String
strFIND = InputBox("Enter the field name fragment:")
Set db = CurrentDb
'Open the Table/Fields table
strSQL = "SELECT * FROM xyzFields WHERE FieldName " & _
"Like '*" & strFIND & "*';"
Set rsXYZFields = db.OpenRecordset(strSQL, dbOpenSnapshot)
With rsXYZFields
.MoveFirst
Do Until .EOF
mTable = Trim(.Fields(0))
mField = Trim(.Fields(1))
strSQL = "INSERT INTO xyzResults ( TableName, " & _
"FieldName ) VALUES ( '" & mTable & "', '" & _
mField & "' )"
CurrentDb.Execute strSQL, dbFailOnError
If Err.Number <> 0 Then MsgBox "Error occurred!!!!"
Err.Clear
.MoveNext
Loop
End With
End Function
--
Ken Snell
<MS ACCESS MVP>
robboll said:
Ken,
After trying to get your code to work, I came up with a hybrid of what
I started and what you are suggesting. It looks like it should work,
but nothing is getting appended. Do you have any suggestions.
Note that xyzFields provides fields: TableName, FieldName and DataType.
And xyzResults is the same structure. The routine is supposed to
append records to xyzResults only when the strFind condition is met.
The entire routine is supposed to look for the value that you key in in
every table in the database. If it finds the value, it appends the
TableName and the FieldName into xyzResults.
I appreciate your help with this.
Function myfun()
On Error Resume Next
Dim db As Database
Dim rsXYZFields As DAO.Recordset
Dim mTable As String
Dim mField As String
Dim strSQL As String
Dim strFIND As String
strFIND = InputBox("Enter the field name fragment:")
Set db = CurrentDb
'Open the Table/Fields table
Set rsXYZFields = db.OpenRecordset("xyzFields", dbOpenSnapshot)
With rsXYZFields
.MoveFirst
Do Until .EOF
mTable = "[" & Trim(.Fields(0)) & "]"
mField = "[" & Trim(.Fields(1)) & "]"
strSQL = "INSERT INTO xyzResults SELECT from " & mTable &
" where " & mField & " Like " & "'*'" & strFIND & "'*'"
CurrentDb.Execute strSQL, dbFailOnError
.MoveNext
Loop
End With
End Function