Article ID: 136062
Article Last Modified on 1/19/2007
182568 ACC97: Microsoft Access 97 Sample Queries Available in Download Center
Field Type Type-Casting Character
------------------------------------------
Text Single quotation mark (')
Date Number sign (#)
Numeric None
The following sample SQL statements demonstrate the use of type casting.
Text: "Select * from [Orders] where [ShipCity] = '" & [Mycriteria] _ & "' ;" Date: "Select * from [Orders] where [OrderDate] = #"& [Mycriteria] _ & "# ;" Numeric: "Select * from [Orders] where [EmployeeID] = "& [Mycriteria] _ & ";"To create a sample function that uses the QBF technique to display the records matching the specified criteria, follow these steps:
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
Name: cmdRunQuery
Caption: Run Query
Private Sub cmdRunQuery_Click()
Dim db As Database
Dim QD As QueryDef
Dim where As Variant
Set db = CurrentDb()
' 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 [CustomerID].
' Note that there are no type-casting characters surrounding the
' numeric field [EmployeeID].
where = Null
where = where & " AND [ShipCountry]= '" + Me![Ship Country] + "'"
where = where & " AND [EmployeeID]= " + Me![Employee Id]
'NOTE: In Microsoft Access 97, when you use the plus sign (+) in an
'expression in which you are concatenating a variable of the numeric
'data type, you must use parenthesis around the syntax, as in the
'following example:
'
' where = where & (" AND [EmployeeID]= " + Me![Employee Id])
'
'You must also use a conversion function to make sure that the proper
'conversion (to either NULL or String) takes place.
' The following section evaluates the ShipCity criteria you enter.
' If the first or last character of the criteria 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 [ShipCity].
If Left(Me![Ship City],1) = "*" Or Right(Me![Ship City],1) = "*" Then
where = where & " AND [ShipCity] like '" + Me![Ship City] + "'"
Else
where = where & " AND [ShipCity] = '" + Me![Ship City] + "'"
End If
' Note the number signs (#) surrounding the date field [Order Date].
If Not IsNull(Me![Order End Date]) Then
where = where & " AND [OrderDate] between #" + _
Me![Order Start Date] + "# AND #" & Me![Order End Date] & "#"
Else
where = where & " AND [OrderDate] >= #" + Me![Order Start Date] _
+ " #"
End If
' Remove the following MsgBox line if you do not want to display the
' SQL statement.
' NOTE: The Mid function is used in the following MsgBox function to
' remove the word AND that follows the first Where clause. If you do
' not use the Mid function, the SQL statement contains the word AND
' at the beginning of the WHERE clause, for example:
'
' Select * from Orders where AND [CustomerID] = 'CACTU'
MsgBox "Select * from Orders " & (" where " + Mid(where, 6) & ";")
Set QD = db.CreateQueryDef("Dynamic_Query", _
"Select * from orders " & (" where " + Mid(where, 6) & ";"))
DoCmd.OpenQuery "Dynamic_Query"
End Sub
Customer ID text box: CACTU
Ship City text box: BU*
Employee ID text box: 8
Order Start Date text box: 1/1/94
95931 ACC: How to Use the Query-by-Form (QBF) Technique
109334 ACC: How to Dynamically Create a Filter
117544 ACC2: Query by Form (QBF) Using Dynamic QueryDef
Keywords: kbhowto kbprogramming kbusage KB136062