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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
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…

920 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