Autoeng
Why me?
- Local time
- Today, 17:13
- Joined
- Aug 13, 2002
- Messages
- 1,302
I recreated the dynamic query example from Microsoft http://support.microsoft.com/default.aspx?scid=KB;en-us;117544& but when I click the Run Query button it displays a message box showing the SQL WHERE statement and then after I click OK it starts prompting me for the criteria via popup boxes. And I have removed the _'s in the code below per the instructions.
Can anyone think of what I have done incorrectly? I know this is vague but I don't know how else to ask the question.
Autoeng
Can anyone think of what I have done incorrectly? I know this is vague but I don't know how else to ask the question.
Code:
Private Sub Command23_Click()
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"
End Sub
Autoeng
Last edited: