Good morning Steve. Just tried the new code as follows
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim FormName As String
Dim strCrit As String
strCrit = "TAG_NUMBER='" & Me.Tag_Number & "'"
' (replace SearchBox with actual name of textbox where tag number
requested)
Set dbs = DBEngine(0)(0)
For Each tdf In dbs.TableDefs
If tdf.Name Like "tbl*" Then
Set rst = dbs.OpenRecordset("SELECT TAG_NUMBER FROM " &
tdf.Name & " WHERE " & strCrit)
If rst.RecordCount > 0 Then
FormName = "frm" & Mid(tdf.Name, 4)
Exit For
End If
rst.Close
End If
Next tdf
DoCmd.OpenForm FormName, , , strCrit
Set rst = Nothing
Set dbs = Nothing
And I'm getting an error on Set rst = dbs.OpenRecordset("SELECT
TAG_NUMBER FROM " & tdf.Name & " WHERE " & strCrit). The error is: Too
few parameters. Expected 1.
Just a quick thought on what you said about the "hub table". How about
I created a table with 2 fields: TAG_NUMBER which would contain all the
tag numbers from each table and FORM_NAME which would contain the name
of the form that should open. Would that be easier? I would still
have my individual forms referencing the coinsiding table with the
filter set to [Tag_Number] like '41-pit-2055'.
Cathy
Steve said:
Cathy,
As for the underscore in the field name, that's cool. As long as this
is the exact spelling of the field name in *all* of the affected tables,
and as long as we do the same in the code, we're fine with that. Always
best, though, when asking for help on the newsgroups, to provide the
correct information from the beginning, because attention to these
little details is all the helpers have to go on, so it gets confusing
when names get changed mid-stream.
Having said that, I forgot about the hidden system tables. Suggested
amendment to code below...
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim FormName As String
Dim strCrit As String
strCrit = "TAG_NUMBER='" & Me.SearchBox & "'"
' (replace SearchBox with actual name of textbox where tag number
requested)
Set dbs = DBEngine(0)(0)
For Each tdf In dbs.TableDefs
If tdf.Name Like "tbl*" Then
Set rst = dbs.OpenRecordset("SELECT TAG_NUMBER FROM " & tdf.Name
& " WHERE " & strCrit)
If rst.RecordCount > 0 Then
FormName = "frm" & Mid(tdf.Name, 4)
Exit For
End If
rst.Close
End If
Next tdf
DoCmd.OpenForm FormName, , , strCrit
Set rst = Nothing
Set dbs = Nothing
As regards your table structure, from what you told me this is a valid
sub-typing type of design, and if the fields used by the 22 different
groupings are indeed significantly different from each other, then it is
probably correct to have 22 separate tables. However, this type of
design would usually have a "hub" table, which contains the Tag_Number
field for *all* records from *all* sub-typed tables, and also any other
fields which are common to all existing 22 tables (it would be unusual
if there was absolutely none). And this hub table would also have an
additional field (Category or some such) which identifies which of the
22 groupings each record is associated with. If you did that, you would
find this type of data searching, and querying, and opening the
appropriate forms or reports, etc, would be a lot simpler, because most
of the grouping and selecting etc is based on the data in the one
central table.