Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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
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.

Question by:fester62
  • 3
  • 3
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?

Author Comment

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.

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

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.


Author Comment

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 ?

LVL 65

Accepted Solution

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.

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

860 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