The information in this article applies to:
SUMMARYThis article explains how to create and use a parameter query using Data Access Objects (DAO). A parameter query is a type of QueryDef specific to the Microsoft Jet database engine used by Visual Basic, Microsoft Access, and other products. Parameter queries enable you to automate the process of changing query criteria. With a parameter query, you can set new values for the parameters each time you run the query. MORE INFORMATIONA parameter query is created in a program by using the CreateQueryDef() function. Following is the syntax for the CreateQueryDef() function: Set querydef = database.CreateQueryDef(name, sqltext)The sqltext string is optional or it can be defined by using the .SQL property of the QueryDef. To create a parameter query, place the PARAMETERS statement in the sqltext string. Here is the syntax for the PARAMETERS statement: PARAMETERS parametertext datatypeThe following table lists the appropriate Microsoft Jet SQL data type that should be used with the PARAMETERS statement as well as the corresponding Microsoft Access field type, Visual Basic variable type, and constant value from the DATACONS.TXT file. NOTE: Visual Basic 4.0 and later and Office 95 and later versions of Basic support additional data types (Boolean, Byte, Byte Array). The table below lists equivalent Basic types for earlier versions of Basic:
Following the PARAMETERS statement in the sqltext string, place the query.
The query can refer to the parameter (parametertext) named in the
PARAMETERS statement. Wherever the query refers to a parameter, the current
value will be substituted when the query is executed.
For example, if the query text is: PARAMETERS i SHORT; SELECT fld FROM tbl WHERE fld=iand the parameter i was set to 42 in the program, the parameter i would be substituted and the resulting query would be equivalent to: SELECT fld FROM tbl WHERE fld=42 Multiple Parameters in a PARAMETERS StatementIt is also possible to have multiple parameters in a PARAMETERS statement. To do this, use commas to separate the parameters as follows:PARAMETERS parametertext datatype, parametertext datatype, ...Prior to executing the query, set the parameters using this syntax: querydef!parametertext = valueIn the previous example, you would use QD!i=42 before executing the query. Once the parameters are set, you are ready to execute the query. There are three methods (Execute, CreateDynaset, and CreateSnapshot) supported by a QueryDef that will cause the query to be executed. Example Parameter QueriesThe following example illustrates the use of a short parameter in a query. The example has two parts. The first part creates a new QueryDef for BIBLIO.MDB (the sample Microsoft Access database that ships with Visual Basic) and should be executed only once. The second part uses the QueryDef to create a snapshot, which is then displayed. To test the example, place each of the following code segments in a CommandButton Click event procedure.NOTE: There are some minor differences between the older DAO syntax used by Visual Basic 3.0 and Access 1.x and the newer syntax used by the other products this article applies to. Old and New DAO syntax are indicated by comments:
The second example shows how to use an action parameter query. Note that
action queries are invoked with the Execute method, not CreateDynaset or
CreateSnapshot, because they do not return records:
REFERENCESMore information on parameter queries is available in the Visual Basic, version 3.0 and 4.0, "Professional Features Book 2." Additional query words: parameterized querydefs
Keywords : kbprg kbVBp500 VB4WIN VBKBDAO vbwin |
Last Reviewed: November 3, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |