Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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

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
fester62
Asked:
fester62
  • 3
2 Solutions
 
Arthur_WoodCommented:
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
 
fester62Author Commented:
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
 
Arthur_WoodCommented:
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
 
bknouseCommented:
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
 
Arthur_WoodCommented:
why only a B.  What additional information or assistance would you like?

Glad to be of assistance.

AW
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now