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
789 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now