Solved

Where should I place QUERYDEFS so they can be accessed from a form module (they are referenced several times by form code).

Posted on 2007-12-05
5
268 Views
Last Modified: 2013-11-27
I have a form and have coded my queries using VBA. At the moment I delete them and re-create them everytime I run them as a timekey parameter may change plus the user may run the tool several times and an error would occur if QUERYDEF.CREATE method finds that the query already exists. I basically want to create the queries once in code and be able to call them with a timekey parameter at any time during the running of the programmme.

Will querdefs persist after the application closes down ? If so that would give me an error the next time the tool is opened (hence I delete and recreate everytime) and the code runs to create the queries.

 I could just set up the objects once and leave them out of the VBA code I suppose, but ideally I want the code to include every object used. Am I going about this the right way ? I havent used QUERYDEFS before so am not sure where they shouold be placed so they are created once and can theb be called several times passing in a timekey(date) parameter.

Tony
0
Comment
Question by:fester62
  • 3
5 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 100 total points
ID: 20410706
The actual QWuery Def is stored as a Query in the MDB - you will see them if you open the Query Tab in the Databse View.  Yes, they persist when the Application is shut down.

If you define the Query so that the TimeKey is a parameter of the query, then yes, you can run the same query and provide a new timekey input value.

AW
0
 

Author Comment

by:fester62
ID: 20410827
Would I need code that runs immediately the application is opened that checks for the existence of the queries, if they dont exists it creates them, or would you recommend keeping the query definitions outside of VBA code and then just calling them by name with the paramter as required ? These are queries that will be re-run daily so would be classified as permanent in nature.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20411014
Try the second choice, and see if that satifies your requirements.  That will save you some execution cycles.  But the First choice will always work - just costs a bit of performance.

AW
0
 
LVL 6

Assisted Solution

by:bknouse
bknouse earned 25 total points
ID: 20411513
Please be aware that a querydef is an object that can be manipulated.  For example, I have a pass-through query connected via ODBC to a back-end IBM DB2 database.  Instead of creating multiple instances of the same type of query, I manipulate the sql property of the querydef.  The syntax to modify the sql (where sqlZ is a string variable holding the sql statement):

CurrentDb.QueryDefs("nameOfQuery").sql = sqlZ

Brent
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 20808542
why only a B.  What additional information or assistance would you like?

Glad to be of assistance.

AW
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

12 Experts available now in Live!

Get 1:1 Help Now