Solved

How do you create and store a SELECT query in VBA, QUERYDEF does not seem to apply to select query definitions

Posted on 2007-12-06
6
791 Views
Last Modified: 2013-11-27
I have a complex query that uses other queries which should run off user-supplied dates. I want to create the base queries using a date paramter and store them - then use them in the comples query joins.

Access does not appear to allow you to store a SELECT query using the QUERYDEF statement.

Tony
0
Comment
Question by:fester62
  • 3
  • 3
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20420880
This sounds like way too much work.

What is the requirement for creating these queries via VBA code, and not pre-creating them manually?
0
 

Author Comment

by:fester62
ID: 20420927
I tried doing that originally but the queries run off a data parameter. I can specify a parameter in the query designer but then that pops up a box when the query runs - whereas i want the query to use txtRunDate supplied on a Form. - I am not sure how to get the query to work off that date and not prompt the user, without using code.

your help is appreciated.

tony
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20420963
>whereas i want the query to use txtRunDate supplied on a Form.
In that case, just feed that value in your query. (Below is air code, rename the obvious)  

{SQL View}
SELECT blah, blah, blah
FROM blah
WHERE SomeDate = [Forms]![SomeFormName]![SomeTextboxName]

{Design View}
In the column that has the date you want to compare to your form field, enter this in the criteria row
[Forms]![SomeFormName]![SomeTextboxName]

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:fester62
ID: 20421497
Ok, I can see how that should work, actually instead of a textbox i want it to pick up a DTpicker1.value, but when I susbitute that into your suggested code ti does not work (for either a textbox or a DTpicker), do i need to enclose with '#' or some other formatting ? Cant get that to work either.

tony
0
 

Author Comment

by:fester62
ID: 20421533
ok, I have it working off a textbox now, just had the date in the wrong format. Now I need to get it working with a dtpicker control. Should there be a difference, cant see why ?

tony
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 125 total points
ID: 20422426
>do i need to enclose with '#' or some other formatting ?
If the SQL is pieced together by VBA, yes, otherwise no.  
Just to be safe though, in case the form control value is being read into the query as text, use CDate... CDate([Forms]![SomeFormName]![SomeTextboxName])

This also assumes that the form is open, and the value in SomeTextboxName is in date format.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question