tags:

views:

1331

answers:

1

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.

A: 

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.

Remou
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?
Nick S.
If you could post an example of how to properly run a Query in VBA that would really be all I need.
Nick S.
The line I posted should work. Does the query Readings2009 work when you open it manually.
Remou
Well, for one, there is no Today() function in Jet/Access. You want Date() instead.
David-W-Fenton
Ok I got the Remou's suggestion to work but I need to display this Query in a new form. Any ideas?
Nick S.
You can select from: set up your form based on the query; update the query with new sql string; update the form's record source with new sql string.
Remou
Thanks I appreciate the help.
Nick S.