I have query that runs off the following code. When a user selects too many Models, I get the Query is Too Complex error. I know why, it is because the user selects too many models and the query creats loads of "OR" statements. I found that there is a limit of 249.
This is the VBA Code that runs the query and opens the report:
Code:
Private Sub Command55_Click()
Dim varItm As Variant
Dim ModelWhere As String
Dim strQuery
Dim LowPop As String
Dim SDate As Integer
Dim EDate As Integer
Dim qdf As DAO.QueryDef
If SysCmd(acSysCmdGetObjectState, acQuery, "SUMCascadeQry") = acObjStateOpen Then
DoCmd.Close acQuery, "SUMCascadeQry"
End If
Set qdf = CurrentDb.QueryDefs("SUMCascadeQry")
LowPop = InputBox("Please Enter Minimum Population", "Population")
SDate = InputBox("Please Enter First Year", "Year")
EDate = InputBox("Please Enter Last Year", "Year")
strQuery = "SELECT * FROM dbo_BAMtbl WHERE ((dbo_BAMtbl.Pop)>=" & LowPop & " AND "
For Each varItm In Me.ListModu.ItemsSelected
If ModelWhere = "" Then
ModelWhere = "[dbo_BAMtbl].[Model]=" & _
Chr(34) & Me.ListModu.Column(0, varItm) & Chr(34)
Else
ModelWhere = ModelWhere & " OR [dbo_BAMtbl].[Model]=" & _
Chr(34) & Me.ListModu.Column(0, varItm) & Chr(34)
End If
Next varItm
strQuery = strQuery & "(" & ModelWhere & "));"
qdf.SQL = strQuery
DoCmd.OpenReport "CascadeSUMrpt", acViewPreview, , "Year([Date]) >=" & SDate & " And Year([Date]) <= " & EDate
'DoCmd.OpenQuery ("SUMCascadeQry")
'MsgBox (strQuery)
Set qdf = Nothing
End Sub
This is the query and where the error happens:
Code:
SELECT *
FROM dbo_BAMtbl
WHERE ((dbo_BAMtbl.Pop)>=0 AND ([Model]="DV23" OR [Model]="DV03" OR [Model]="DV2512" OR [Model]="DV8001"));
There are over a million DV Model types possible. When too many are chose, that "OR" statement goes on 'til it over loads. I don't know the limit, but I believe there is a limit to the number of "OR" statements a query can use.
The .Pop is an input box in the form and the Model choices are from cascading list boxes also in the form, as you can see from the VBA above.
I had been thinking about a "Create Table" temporary table query, but I think that will still cause that multiple "OR" statement problem.
This project is getting way past my experience and I need help badly, so thank you in advance for any help I can get!