Article ID: 117544
Article Last Modified on 7/8/2002
Field Type Type-Casting Character
------------------------------------------
Text Single quotation mark (')
Date Number sign (#)
Numeric None
Text: "Select * from [Orders] where [ship city] = '" & [Mycriteria] _ & "' ;" Date: "Select * from [Orders] where [Order Date] = #"& [Mycriteria] _ & "# ;" Numeric: "Select * from [Orders] where [Employee ID] = "& [Mycriteria] _ & ";"
Text Box 1:
Name: Customer ID
Text Box 2:
Name: Ship City
Text Box 3:
Name: Ship Country
Text Box 4:
Name: Employee ID
Text Box 5:
Name: Order Start Date
Text Box 6:
Name: Order End Date
Caption: Run Query
Dim db As Database
Dim QD As QueryDef
Dim MyWhere As Variant
Set db = dbengine.workspaces(0).databases(0)
' Delete the existing dynamic query; trap the error if the query does
' not exist.
On Error Resume Next
db.querydefs.Delete ("Dynamic_Query")
On Error GoTo 0
' Note the single quotation marks surrounding the text fields _
' [Ship Country] and [Customer ID].
' Note that there are no type-casting characters surrounding the _
' numeric field [Employee Id].
MyWhere = Null
MyWhere = MyWhere & (" AND [Ship Country]= '" + _
Me![Ship Country] + "'")
MyWhere = MyWhere & (" AND [Customer Id]= '" + _
Me![customer id] + "'")
MyWhere = MyWhere & (" AND [Employee Id]= " + Me![Employee Id])
' The following section evaluates the Ship City criterion you enter.
' If the first or the last character of the criterion is the wildcard
' character (*), then the function uses the "LIKE" operator in the
' SQL statement instead of "=." Also note the single quotation
' marks surrounding the text field [Ship City].
If Left(Me![Ship City],1) = "*" Or Right(Me![Ship City],1) = "*" Then
MyWhere = MyWhere & (" AND [Ship City] like '" + Me![Ship City] _
+ "'")
Else
MyWhere = MyWhere & (" AND [Ship City] = '" + Me![Ship City]+"'")
End If
' Note the number signs (#) surrounding the date field [Order Date].
If Not IsNull(Me![order start date]) And _
Not IsNull(Me![order end date]) Then
MyWhere = MyWhere & (" AND [order date] between #" & _
Me![order start date] + "# AND #" + Me![order end date] + "#")
ElseIf IsNull(Me![order end date]) Then
MyWhere = MyWhere & (" AND [order date] >= #" + _
Me![order start date] + " #")
ElseIf IsNull(Me![order start date]) Then
MyWhere = MyWhere & (" AND [order date] <= #" + _
Me![order end date] + " #")
End If
' Remove the following MsgBox line if you do not want to display the
' SQL statement.
MsgBox "Select * from orders " & (" where " + Mid(MyWhere, 6) + ";")
Set QD = db.CreateQueryDef("Dynamic_Query", "Select * from _
orders " & (" where " + Mid(MyWhere, 6) + ";"))
DoCmd OpenQuery "Dynamic_Query"
Ship City text box: SE*
Employee ID text box: 1
Customer ID text box: CACTU
Order Start Date text box: 1/1/94
Keywords: kbhowto kbprogramming KB117544