0

So I have a few Queries already written and my goal is to have a user input certain fields that would change the way the Query is returned, basically having the user change 2 or 3 parameters of the original Query.
First, I'm having problems getting a Query to execute in VBA:

Private Sub QResultButton_Click()
DoCmd.OpenQuery (Readings2009, acViewPreview,acReadOnly)
End Sub  

[Readings2009 is a Query I created in Access] This returns a syntax error.
I have also see this done:

strSQL = "SELECT Readings2009.id, Readings2009.othercolumn, Readings2009.another 
          WHERE Readings2009.something > today() ..."
DoCmd.RunSQL (strSQL)

I can't get either to work. Any help would be greatly appreciated.

1 Answer 1

0

In this example, the name of the query must be a string, that is, it must be in quotes:

DoCmd.OpenQuery "Readings2009"

See: http://msdn.microsoft.com/en-us/library/aa220295(office.11).aspx

The other example is for running action queries,not for opening queries to view them.

It would be best to create a form and to use that to display your query.

Sign up to request clarification or add additional context in comments.

6 Comments

Either of the following still returns a syntax error. DoCmd.OpenQuery "Readings2009" or DoCmd.OpenQuery ("Readings2009", acViewPreview,acReadOnly) do I need to define this as a string first?
If you could post an example of how to properly run a Query in VBA that would really be all I need.
The line I posted should work. Does the query Readings2009 work when you open it manually.
Well, for one, there is no Today() function in Jet/Access. You want Date() instead.
Ok I got the Remou's suggestion to work but I need to display this Query in a new form. Any ideas?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.