Hi Everyone
I have a text box on a form that rolls up selections to form a criteria statement for a query.
I tried calling it directly but got a "to complex to evaluate" error so I built the following VBA code to generate the query with a SQL statement.
If no checkboxes are selected the Query runs fine (else portion of the If statement), but when the Then portion of the if statement is run the query builds fine but returns no data. I highlighted the staement referencign the text box in red.
Here is the VBA code:
Private Sub Command42_Click()
Dim db As DAO.Database
Dim qdf As QueryDef
Dim strQuery As String
Dim VarTxt As String
Set db = CurrentDb
Set qdf = db.QueryDefs("Customer_Data_Requested")
If ([Forms]![Data_Selections]![Chk43] = True) Then
Set db = CurrentDb
Set qdf = db.QueryDefs("Customer_Data_Requested")
If Text32.Value > "" Then
VarTxt = Text32.Value
End If
strQuery = "SELECT [Customer Level DATA].[CUSTOMER COID], [Customer Level DATA].[CUSTOMER NUMBER], [Customer Level DATA].[CUSTOMER NAME], " & _
"[Customer Level DATA].[XAA TOTAL FEES], [Customer Level DATA].[WF TOTAL FEES], [Customer Level DATA].[TOTAL FEES DIFFERENCE]" & _
"FROM [Customer Level DATA]" & _
"WHERE ((([Customer Level DATA].[CUSTOMER COID]) Like IIf([Forms]![Data_Selections]![Chk2] = True, [Forms]![Data_Selections]![Combo0], '*'))" & _
"And (([Customer Level DATA].[CUSTOMER NUMBER]) Like IIf([Forms]![Data_Selections]![Chk6] = True, [Forms]![Data_Selections]![Combo4], '*'))" & _
"AND (([Customer Level DATA].[TOTAL FEES DIFFERENCE]) Like IIf([Forms]![Data_Selections]![Chk43] = True, [Forms]![Data_Selections]![text32], '*')))"
Else
strQuery = "SELECT [Customer Level DATA].[CUSTOMER COID], [Customer Level DATA].[CUSTOMER NUMBER], [Customer Level DATA].[CUSTOMER NAME], " & _
"[Customer Level DATA].[XAA TOTAL FEES], [Customer Level DATA].[WF TOTAL FEES], [Customer Level DATA].[TOTAL FEES DIFFERENCE]" & _
"FROM [Customer Level DATA]" & _
"WHERE ((([Customer Level DATA].[CUSTOMER COID]) Like IIf([Forms]![Data_Selections]![Chk2] = True, [Forms]![Data_Selections]![Combo0], '*'))" & _
"And (([Customer Level DATA].[CUSTOMER NUMBER]) Like IIf([Forms]![Data_Selections]![Chk6] = True, [Forms]![Data_Selections]![Combo4], '*')))"
End If
Debug.Print strQuery
qdf.SQL = strQuery
DoCmd.OpenQuery "Customer_Data_Requested"
Set qdf = Nothing
Set db = Nothing
End Sub
I thought maybe the text box was not being read right so I added a variable (VarTxt) so I could see what VBA was saying the value was. When I debugged and it came back correctly.
The textbox builds a Query criteria statement that looks like "<=-1000 Or Between -500 and -999.99 Or Between -250 and -499.99" and has 12 options.
Here is the Test Box control source in case that helps:
=IIf([Chk8]=True,"<=-1000") & IIf([Chk8]=True And [Chk10]=True," Or ") & IIf([Chk10]=True,"Between -500 and -999.99") & IIf([Chk12]=True And ([Chk8]=True Or [Chk10]=True)," Or ") & IIf([Chk12]=True,"Between -250 and -499.99","") & IIf([Chk30]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True)," Or ","") & IIf([Chk30]=True,"Between -100 and -249.99","") & IIf([Chk28]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True)," Or ","") & IIf([Chk28]=True,"Between -50 and -99.99","") & IIf([Chk26]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True)," Or ","") & IIf([Chk26]=True,"Between -25 and -49.99","") & IIf([Chk22]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True)," Or ","") & IIf([Chk22]=True,">=1000","") & IIf([Chk20]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True)," Or ","") & IIf([Chk20]=True,"Between 500 and 999.99","") & IIf([Chk18]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True)," Or ","") & IIf([Chk18]=True,"Between 250 and 499.99","") & IIf([Chk16]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True Or [Chk18]=True)," Or ") & IIf([Chk16]=True,"Between 100 and 249.99") & IIf([Chk14]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True Or [Chk18]=True Or [Chk16]=True)," Or ") & IIf([Chk14]=True,"Between 50 and 99.99","") & IIf([Chk24]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True Or [Chk18]=True Or [Chk16]=True Or [Chk14]=True)," Or ","") & IIf([Chk24]=True,"Between 25 and 49.99")
Does anyone have any idea why the Query with the text box data called will not return data?
Thanks for the help!
I have a text box on a form that rolls up selections to form a criteria statement for a query.
I tried calling it directly but got a "to complex to evaluate" error so I built the following VBA code to generate the query with a SQL statement.
If no checkboxes are selected the Query runs fine (else portion of the If statement), but when the Then portion of the if statement is run the query builds fine but returns no data. I highlighted the staement referencign the text box in red.
Here is the VBA code:
Private Sub Command42_Click()
Dim db As DAO.Database
Dim qdf As QueryDef
Dim strQuery As String
Dim VarTxt As String
Set db = CurrentDb
Set qdf = db.QueryDefs("Customer_Data_Requested")
If ([Forms]![Data_Selections]![Chk43] = True) Then
Set db = CurrentDb
Set qdf = db.QueryDefs("Customer_Data_Requested")
If Text32.Value > "" Then
VarTxt = Text32.Value
End If
strQuery = "SELECT [Customer Level DATA].[CUSTOMER COID], [Customer Level DATA].[CUSTOMER NUMBER], [Customer Level DATA].[CUSTOMER NAME], " & _
"[Customer Level DATA].[XAA TOTAL FEES], [Customer Level DATA].[WF TOTAL FEES], [Customer Level DATA].[TOTAL FEES DIFFERENCE]" & _
"FROM [Customer Level DATA]" & _
"WHERE ((([Customer Level DATA].[CUSTOMER COID]) Like IIf([Forms]![Data_Selections]![Chk2] = True, [Forms]![Data_Selections]![Combo0], '*'))" & _
"And (([Customer Level DATA].[CUSTOMER NUMBER]) Like IIf([Forms]![Data_Selections]![Chk6] = True, [Forms]![Data_Selections]![Combo4], '*'))" & _
"AND (([Customer Level DATA].[TOTAL FEES DIFFERENCE]) Like IIf([Forms]![Data_Selections]![Chk43] = True, [Forms]![Data_Selections]![text32], '*')))"
Else
strQuery = "SELECT [Customer Level DATA].[CUSTOMER COID], [Customer Level DATA].[CUSTOMER NUMBER], [Customer Level DATA].[CUSTOMER NAME], " & _
"[Customer Level DATA].[XAA TOTAL FEES], [Customer Level DATA].[WF TOTAL FEES], [Customer Level DATA].[TOTAL FEES DIFFERENCE]" & _
"FROM [Customer Level DATA]" & _
"WHERE ((([Customer Level DATA].[CUSTOMER COID]) Like IIf([Forms]![Data_Selections]![Chk2] = True, [Forms]![Data_Selections]![Combo0], '*'))" & _
"And (([Customer Level DATA].[CUSTOMER NUMBER]) Like IIf([Forms]![Data_Selections]![Chk6] = True, [Forms]![Data_Selections]![Combo4], '*')))"
End If
Debug.Print strQuery
qdf.SQL = strQuery
DoCmd.OpenQuery "Customer_Data_Requested"
Set qdf = Nothing
Set db = Nothing
End Sub
I thought maybe the text box was not being read right so I added a variable (VarTxt) so I could see what VBA was saying the value was. When I debugged and it came back correctly.
The textbox builds a Query criteria statement that looks like "<=-1000 Or Between -500 and -999.99 Or Between -250 and -499.99" and has 12 options.
Here is the Test Box control source in case that helps:
=IIf([Chk8]=True,"<=-1000") & IIf([Chk8]=True And [Chk10]=True," Or ") & IIf([Chk10]=True,"Between -500 and -999.99") & IIf([Chk12]=True And ([Chk8]=True Or [Chk10]=True)," Or ") & IIf([Chk12]=True,"Between -250 and -499.99","") & IIf([Chk30]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True)," Or ","") & IIf([Chk30]=True,"Between -100 and -249.99","") & IIf([Chk28]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True)," Or ","") & IIf([Chk28]=True,"Between -50 and -99.99","") & IIf([Chk26]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True)," Or ","") & IIf([Chk26]=True,"Between -25 and -49.99","") & IIf([Chk22]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True)," Or ","") & IIf([Chk22]=True,">=1000","") & IIf([Chk20]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True)," Or ","") & IIf([Chk20]=True,"Between 500 and 999.99","") & IIf([Chk18]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True)," Or ","") & IIf([Chk18]=True,"Between 250 and 499.99","") & IIf([Chk16]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True Or [Chk18]=True)," Or ") & IIf([Chk16]=True,"Between 100 and 249.99") & IIf([Chk14]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True Or [Chk18]=True Or [Chk16]=True)," Or ") & IIf([Chk14]=True,"Between 50 and 99.99","") & IIf([Chk24]=True And ([Chk8]=True Or [Chk10]=True Or [Chk12]=True Or [Chk30]=True Or [Chk28]=True Or [Chk26]=True Or [Chk22]=True Or [Chk20]=True Or [Chk18]=True Or [Chk16]=True Or [Chk14]=True)," Or ","") & IIf([Chk24]=True,"Between 25 and 49.99")
Does anyone have any idea why the Query with the text box data called will not return data?
Thanks for the help!
Last edited: