Option Compare Database
Private Sub btnRunQuery_Click()
On Error GoTo Err_btnRunQuery_Click
Dim MyDatabase As Database
Dim MyQueryDef As QueryDef
Dim where As Variant
Set MyDatabase = CurrentDb()
On Error Resume Next
' Delete the existing dynamic query; trap the error if the query does
' not exist.
'If ObjectExists("Queries", "qryDynamic_QBF") = True Then
MyDatabase.QueryDefs.Delete "qryDynamic_QBF"
MyDatabase.QueryDefs.Refresh
On Error GoTo 0
'End If
' 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 [Productno]= '" + Me![Productno] + "'"
where = where & " AND [Productno] Like '" + Me![Productno] + "*" + "'"
'where = where & " AND [Denomination]= '" + Me![Denomination] + "'"
where = where & " AND [Denomination] Like '" + Me![Denomination] + "*" + "'"
'where = where & " AND [Supplier]= '" + Me![Supplier] + "'"
where = where & " AND [Supplier] Like '" + Me![Supplier] + "*" + "'"
'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![Productno], 1) = "*" Or Right(Me![Productno], 1) = "*" Then
'where = where & " AND [Denomination] like '" + Me![Productno] + "'"
'Else
'where = where & " AND [Productno] = '" + Me![Productno] + "'"
'End If
' Note the number signs (#) surrounding the date field [Order Date].
' 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'
Set MyQueryDef = MyDatabase.CreateQueryDef("qryDynamic_QBF", _
"Select ProductHyper,Alteration,Denomination,Kit,Supplier,Date from Productnr1 " & (" where " + Mid(where, 6) & ";"))
DoCmd.OpenQuery "qryDynamic_QBF"
Exit_btnRunQuery_Click:
Exit Sub
Err_btnRunQuery_Click:
MsgBox Err.Description
Resume Exit_btnRunQuery_Click
End Sub
Private Sub btnRunQuery_Enter()
End Sub