Passing a boolean operator string to a stored query

GKIL67

Registered User.
Local time
Today, 08:37
Joined
Apr 28, 2011
Messages
24
I have a stored query.
I want to run this query via a button from a form and pass
a boolean such as "6 OR 8 OR 10" that is in a txtField on the form.

So, I put in the respective field - in the design grid of the query
this: [Forms]![Form]![txtField]
and in the button: DoCmd.OpenQuery Query, acNormal, acEdit

I know I'm doing it wrong...
What is the easiest way to accomplish this?

I've found a few threads - just can't put it all together... Thanks!
 
You cannot pass SQL language elements to a stored query, only values.

Either write the entire query in code, or just, in code, modify the WHERE clause by using the queryDef object.

If you always have a fixed number of parameters, then you can pass each value, but cannot change their number.

"6 OR 8 OR 10"can also be expressed as MyField IN(6,8,10) but you cannot pass that either.
 
Just to make it clear...
Lets say that there are like 10 numbers to be ORed, that means to build a WHERE clause that keeps on repeating (loop) something like:
strSQL = strSQL & " OR myfield = " & me.myfield.Value

Please advise, Thanks!
 
jdraw,
The "6 OR 8 OR 10" means to restrict the values of the records to those that contain 6 OR 8 OR 10. But these numbers could change either in value or in amount of ORed numbers together.
 
...modify the WHERE clause by using the queryDef object.
If you always have a fixed number of parameters, then you can pass each value, but cannot change their number.
"6 OR 8 OR 10"can also be expressed as MyField IN(6,8,10) but you cannot pass that either.
Solved by following spikepl suggestion above, thanks!

1. Put in the design grid of the query something like: [Forms]![Form]![txtField]
2. Then use a sub/function like:
Code:
Sub dummy
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
    
Dim stDocName As String
Dim strSQL As String, strWHERE

stDocName = "myQuery"

'Build the strWHERE clause
   'This is where the MyField IN(6,8,10) comes.

'Then build the strSQL

'Finally you end up with something like:
 strSQL="SELECT tbl.field1, tbl.field2
FROM tbl " & strWHERE & "=[Forms]![Form]![txtField]));"

'Get a handle to that query (QueryDef object).
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs(stDocName)
    
'Change the SQL property of the query through the QueryDef object.
qdf.SQL = strSQL
    
'Open the query with its new (modified) SQL statement.
DoCmd.OpenQuery stDocName, acNormal, acEdit

'Clean up
Set qdf = Nothing
Set dbs = Nothing

End Sub
 

Users who are viewing this thread

Back
Top Bottom