Open Query/SQL

ingrid

Registered User.
Local time
Today, 09:47
Joined
Apr 10, 2003
Messages
42
I want to open a query which is build in VBA. I tryed Docmd.OpenQuery en Docmd.RunSQL but both didn't work.

My code:
If field1.value = True then
SQL = SQL & "field1, "
End If

If field2.value = True then
SQL = SQL & "field2, "
End If

etc.

SQL = Left(SQL, Len(SQL) - 2)
SQL = "SELECT " & SQL & " FROM Table "
'DoCmd.OpenQuery (SQL)
'DoCmd.RunSQL (SQL)

Please help me :confused:
 
ingrid said:
DoCmd.OpenQuery (SQL)
'DoCmd.RunSQL (SQL)

Please help me :confused:

DoCmd.OpenQuery (SQL)

This won't work as you need to specify an actual query object - an SQL string is just that; a string.

DoCmd.RunSQL (SQL)

This won't work as the RunSQL statement only "runs" action queries - APPEND, DELETE, MAKE TABLE, etc and not a simple SELECT query.

What you need to do is make a QueryDef - the help files should give you an idea on what to do as will searching the forum as there are a few QueryDef examples...
 

Users who are viewing this thread

Back
Top Bottom