Roger Withnell said:
My query has a WHERE clause on a column (no surprise there!).
When the query is run, I would like the query to ask the user to
enter field name of this column .
How do I do this?
About the only way you can do this is to rewrite the SQL of the query on
the fly. You can't have the query itself prompt for the name of the
column. But you can use the InputBox function or your own dialog form
to prompt the user for the name, and then modify the SQL of the query.
The code to do that might look something like the (completely untested,
"air code") example below. Note that this example assumes that you
already know what the basic SQL of the query is, and what criterion will
be applied to the field entered by the user; all you don't know is
which field it will be applied to. Here's the example:
'----- start of example -----
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strColumnName As String
strColumnName = InputBox("Enter the field name:")
If Len(strColumnName) = 0 Then
MsgBox "No field name, no query!"
Exit Sub
End If
strSQL = "SELECT foo, bar, baz FROM MyTable WHERE " & _
strColumnName & _
" > 0"
Set db = CurrentDb
Set qdf = db.QueryDefs("MyQuery")
qdf.SQL = strSQL
Set qdf = Nothing
Set db = Nothing
DoCmd.OpenQuery "MyQuery"
'----- end of example -----
If there are more variables to the situation, it can get rather hairy.
Out of curiosity, why do you need to do this? It's pretty unusual that
you'd need to apply the same criterion to any one of several possible
fields, and not know in advance which field. What is the table
structure like that it needs this sort of operation?