Hi...
Logic: This procedure is as 'generic' as possible so as to use the same process of creating queries in any number of different databases. While the SQL Statement may change, the criterion is always based on the value of the Global Variable. It is a modularized approach to help minimize the developement. Simply stated, one single Table in each of the varying databases holds the SQL statements that are necessary for creating the queries for that particular database. Rather than writing QueryDefs for each and every database, I simply modify the SQL Statements in the particular Table. A 'single' procedure can then call the 'customized' SQL statements and create the necessary queries in any one of my 10 databases. (If you have a better way... I am willing to listen.)
Why do it this way? Experience has proven that "Select Queries" run much faster when the exact criterion (in this case, Client_No) is on the criterion line as opposed to basing the criterion on a Field on a Form. Eg.
SELECT tb_TableName.Client_No FROM tb_TableName
WHERE (((tb_TableName.Client_No)='631107547'));
runs MUCH faster than...
SELECT tb_TableName.Client_No
FROM tb_TableName
WHERE (((tb_TableName.Client_No)=[Forms]![FormName]![txtFieldName]));
A Client is selected in a Dropdown Box, and the value of the Client is then set to a Global Variable. This value can then used throughout the database. In this case, I want the Global Variable to become the "Where" criterion for the Select Query (or a complete SQL Statement) in the Table.
The following SQL Statement when called from the Table using the function WORKS. It creates the desired Query.
SELECT tb_TableName.Client_No FROM tb_TableName
WHERE (((tb_TableName.Client_No)='631107547'));
The following SQL Statement when called from the Table does NOT work.
SELECT tb_TableName.Client_No FROM tb_TableName
WHERE (((tb_TableName.Client_No)='" & GlobalVariableHere & "'));
Yes, I have checked the Debug Window and the Syntax looks correct. It creates the Select Query but the syntax in the Criterion remains '" & GlobalVariable & "' not '631107547' Consequently, it returns no value(as you so correctly pointed out at the very begining..."Variables are VBA objects and cannot be used directly by SQL...")
Hope this helps.
Pat