Article ID: 142938
Article Last Modified on 10/11/2006
Set querydef = database.CreateQueryDef(name, sqltext)
querydef - a QueryDef object
database - a Database object
name - string containing query name
sqltext - string containing the SQL query text
PARAMETERS parametertext datatype
parametertext - name of the parameter
datatype - type of the parameter
Microsoft Microsoft Visual Access SQL Access Field Basic Type DATACONS.TXT Constant --------------------------------------------------------------------- Bit Yes/No Integer/Boolean DB_BOOLEAN = 1 Byte Byte Integer/Byte DB_BYTE = 2 Short Integer Integer DB_INTEGER = 3 Long Long Integer Long DB_LONG = 4 Currency Currency Double DB_CURRENCY = 5 IEEESingle Single Single DB_SINGLE = 6 IEEEDouble Double Double DB_DOUBLE = 7 DateTime Date/Time Variant DB_DATE = 8 Binary Binary String/Byte Array Text Text String DB_TEXT = 10 LongBinary OLE Object String/Byte Array DB_LONGBINARY = 11 LongText Memo String DB_MEMO = 12Following 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.
PARAMETERS i SHORT; SELECT fld FROM tbl WHERE fld=i
SELECT fld FROM tbl WHERE fld=42
PARAMETERS parametertext datatype, parametertext datatype, ...
querydef!parametertext = value
querydef - a QueryDef object
parametertext - the name of the parameter in the PARAMETERS statement
value - the value the parameter will have
'Create QueryDef "by date"
Dim Db As Database
Dim Qd As QueryDef
Set Db = OpenDatabase("C:\VB\BIBLIO.MDB") ' Old
Set Db = DBEngine(0).OpenDatabase("C:\VB4-32\BIBLIO.MDB") ' New
Set Qd = Db.CreateQueryDef("By date") 'Create the query "By date"
QdText = "PARAMETERS dp Short; "
QdText = QdText & "SELECT * from Titles WHERE [Year Published] = dp"
Qd.SQL = QdText
Print Qd.SQL
Qd.Close
' Create Snapshot from QueryDef
Dim Rs As Snapshot ' Old
Set Qd = Db.OpenQueryDef("By Date") ' Old
Dim Rs As Recordset ' New
Set Qd = Db.QueryDefs("By Date") ' New
Qd!dp = 1991 'Set the value of the dp parameter
Set Rs = Qd.CreateSnapshot() ' Old
Set Rs = Qd.OpenRecordset(dbOpenSnapshot) ' New
Do Until Rs.EOF
For i = 1 To Rs.Fields.Count - 1
Print Rs(i); 'Display results of query
Next
Print
Rs.MoveNext
Loop
Rs.Close
Qd.Close
Db.Close
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:
'Create QueryDef
Dim Db As Database
Dim Qd As QueryDef
Set Db = OpenDatabase("C:\VB\BIBLIO.MDB") ' Old
Set Db = DBEngine(0).OpenDatabase("C:\VB4-32\BIBLIO.MDB") ' New
Set Qd = Db.CreateQueryDef("Delete by name") 'Create the query
QdText = "PARAMETERS p1 Text; "
QdText = QdText & "DELETE * FROM Authors WHERE Author = p1;"
Qd.SQL = QdText
Print Qd.SQL
Qd.Close
' Execute the QueryDef
Set Qd = Db.OpenQueryDef("Delete by name") ' Old
Set Qd = Db.QueryDefs("Delete by name") ' New
Qd!p1 = "Bob"
Qd.Execute 'Perform the action query
Qd.Close
Db.Close
Additional query words: parameterized querydefs
Keywords: kbhowto kbprogramming KB142938