I'm trying to build a search utility for users to find 'inquiries' in
my database that involves several tables. This is normally easy to do
with the query builder, but I have a unique situation involving a multi
select listbox. Unfortunatly, my SQL skills are somewhat limited, so
I'm not sure if there is an easy way around it. To simplify the
explanation, I'll simplify the table/field setup to get at the meat of
the question.
I have a table with a field called IssueID in table tblMain, and
another field called ProductType in tblProductType. Each IssueID may
have many ProductTypes. tblMain and tblProductType linked together in a
one to many relationship.
Now, I have a search form, which allows users to enter in parameter
query criteria for tblMain. However, criteria for the linked table
tblProductTypes are in a multi select list box. Clicking the 'Find'
button needs to find all matches in tblMain limited to only IssueID's
that contain at least one of the ProductTypes in tblProductType.
It's easy enough to build an SQL statement of the listbox by cycling
through all selected objects and concatenating a string together. The
sticking point is that I need (well, at least I think I need) an actual
object to reference for the INNER JOIN statement in the SQL statement
for tblMain.
I researched some code for QueryDef's, but I'm not sure this will do
the trick. I found the following code in the Help File, which is easy
enough to understand.
Sub NewQuery()
Dim dbs As Database, qdf As QueryDef
Dim strSQL As String
' Return reference to current database.
Set dbs = CurrentDb
' Refresh QueryDefs collection.
dbs.QueryDefs.Refresh
' If RecentHires query exists, delete it.
For Each qdf in dbs.QueryDefs
If qdf.Name = "RecentHires" Then
dbs.QueryDefs.Delete qdf.Name
End If
Next qdf
' Create SQL string to select employees hired on or after 1-1-94.
strSQL = "SELECT * FROM Employees WHERE HireDate >= #1-1-94#;"
' Create new QueryDef object.
Set qdf = dbs.CreateQueryDef("RecentHires", strSQL)
' Open query in Datasheet view.
DoCmd.OpenQuery qdf.Name
Set dbs = Nothing
End Sub
The problem here is that I'm seeing the old query getting deleted and a
new one made each time this would run. I'm thinking that could be a
problem in a multi user environment if two users clicked the 'Find'
button at the same time. I.E., the queryDef is made by User1, but
before the SQL statement executes to reference that new query, User2
has already deleted and replaced the query with their own criteria
based off the multiple objects user2 selected in the listbox.
Is there a way I can do this all in a single SQL statement? Or are my
fears about queryDefs in multi-user environments unfounded?
Thanks in advance for any help or advice any of you may have.