Ray said:
It will be an append query. Each auditor goes thru various training and
certifications. We track them for each auditor. This will put the
certifications for each auditor into a certifications table. I do not enter
this data myself so I need to make a form to make it easier for the
supervisors to enter many records at one time. One text box "fills" the
prikey for the certification to the query, another fills the date, another
the trainer, etc. The query gets all the auditors prikeys from the auditors
table. Sometimes, some auditors are absent for the training. They need to be
excluded from the append query. Hence the need to have a text box for "<>2
And <> 7 And <>18" (The prikey for each missing auditor who was absent).
Everything works just fine except for the "<>2 And <> 7 And <>18" thing. I
have found ways to build this into SelectionCriteria when opening forms and
reports but not directly into queries themselves. Docmd.openquery doesnt
appear to support selection criteria (filter).
Since an INSERT INTO (Append) query is an action query, you
should be using either RunSQL (asynchronous) or the Execute
method (synchronous). Both of those methods accept a string
that contains an SQL statment (instead of the name of a
saved querydef) so you can use code to construct the SQL
statment and then run it.
An outline of the kind of code you could use would look
vaguely like this air code:
Dim wc As String, cond As String, ctnm As String
Dim ctl As Control
Dim db As Database, tdf As TableDef
Dim fn As String
Const SQL As String = "INSERT INTO [name of table] " _
"SELECT flda, fldb, .... "
Set db = CurrentDb
Set tdf = db.TableDefs("name of table"
For Each ctl In Me
If ctl.Tag <> "" Then
If Not IsNull(ctl) Then
fn = "[" & ctl.Tag & "]"
cond = BuildCriteria(fn, tdf.Fields(fn).Type, ctl)
wc = wc & " AND (" & cond & ")"
End If
End If
Next ctl
db.Execute SQL & " WHERE " & Mid(wc, 6)
Note: the code assumes that the controls that can be used
for a criteria must have their Tag property set to the name
of the table field that the criteria applies to.