Hi and Help Please,
I have a form (called SelectionForm)with two combo boxes and one multiselect list box that I use to select criteria to open a report (called Rpt03) via a button.Rpt03 is based on a query that refers to the main table of information.
The combo boxes are:[SelectStatus] and [SelectPriority]
and the multiselect list box is called [SelectCSU]
I have used several lines of code comprising mainly of If Then statements to allow for the case where some or all of the criteria are null. This works fine but now my users want me to ADD FOUR (4) EXTRA COMBOBOXES to the SelectionForm and I shudder to think what the final code will be!! I can do it if I have to but I know that my approach is very primative and I'm praying that a guru out there will have a SHORTER, MORE ELEGENT approach to this problem!
The extra combo boxes are called :
[Discipline] [Engineer] [Contractor] [Area]
Please take a look and you will understand......
In the code, the fields [StatusID], [Priority] and [SystemID] are the related fields on the main table as follows:
SelectionForm Main Table
[SelectStatus] [StatusID]
[SelectPriority] [Priority]
[SelectCSU] [SystemID]
--------------------------------------
Dim cntl As Control
Dim lngCounter As Long
Dim varItem As Variant
Dim strCriteria As String
Set cntl = Me.SelectCSU
lngCounter = 0
If Not IsNull(Me![SelectStatus]) Then
If Not IsNull(Me![SelectPriority]) Then
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
strCriteria = "[StatusID] = " & Me![SelectStatus] & " And [Priority] = " & Me![SelectPriority] & ""
End If
Else
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
strCriteria = "[StatusID] = " & Me![SelectStatus] & ""
End If
End If
Else
If Not IsNull(Me![SelectPriority]) Then
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
strCriteria = "[Priority] = " & Me![SelectPriority] & ""
End If
Else
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
MsgBox "No criteria selected. Displaying all records."
End If
End If
End If
DoCmd.OpenReport "Rpt03", acViewPreview, , strCriteria
'Clear the selections after the report is opened
Dim i As Integer
For i = 0 To (SelectCSU.ListCount - 1)
SelectCSU.Selected(i) = False
Next i
DoCmd.Close acForm, "SelectionForm", acSaveYes
End Sub
-------------------------
There must be another way! Can you imagine having to add four more combo boxes as selection criteria into that monster above?? Yikess!
ZaZa
I have a form (called SelectionForm)with two combo boxes and one multiselect list box that I use to select criteria to open a report (called Rpt03) via a button.Rpt03 is based on a query that refers to the main table of information.
The combo boxes are:[SelectStatus] and [SelectPriority]
and the multiselect list box is called [SelectCSU]
I have used several lines of code comprising mainly of If Then statements to allow for the case where some or all of the criteria are null. This works fine but now my users want me to ADD FOUR (4) EXTRA COMBOBOXES to the SelectionForm and I shudder to think what the final code will be!! I can do it if I have to but I know that my approach is very primative and I'm praying that a guru out there will have a SHORTER, MORE ELEGENT approach to this problem!
The extra combo boxes are called :
[Discipline] [Engineer] [Contractor] [Area]
Please take a look and you will understand......
In the code, the fields [StatusID], [Priority] and [SystemID] are the related fields on the main table as follows:
SelectionForm Main Table
[SelectStatus] [StatusID]
[SelectPriority] [Priority]
[SelectCSU] [SystemID]
--------------------------------------
Dim cntl As Control
Dim lngCounter As Long
Dim varItem As Variant
Dim strCriteria As String
Set cntl = Me.SelectCSU
lngCounter = 0
If Not IsNull(Me![SelectStatus]) Then
If Not IsNull(Me![SelectPriority]) Then
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
strCriteria = "[StatusID] = " & Me![SelectStatus] & " And [Priority] = " & Me![SelectPriority] & ""
End If
Else
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [StatusID] = " & Me![SelectStatus] & " And [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
strCriteria = "[StatusID] = " & Me![SelectStatus] & ""
End If
End If
Else
If Not IsNull(Me![SelectPriority]) Then
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [Priority] = " & Me![SelectPriority] & " And [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
strCriteria = "[Priority] = " & Me![SelectPriority] & ""
End If
Else
If cntl.ItemsSelected.Count <> 0 Then
For Each varItem In cntl.ItemsSelected
lngCounter = lngCounter + 1
If lngCounter = cntl.ItemsSelected.Count Then
strCriteria = strCriteria & " [SystemID] = '" & cntl.Column(0, varItem) & "'"
Else
strCriteria = strCriteria & " [SystemID] = '" & cntl.Column(0, varItem) & "' or"
End If
Next varItem
Else
MsgBox "No criteria selected. Displaying all records."
End If
End If
End If
DoCmd.OpenReport "Rpt03", acViewPreview, , strCriteria
'Clear the selections after the report is opened
Dim i As Integer
For i = 0 To (SelectCSU.ListCount - 1)
SelectCSU.Selected(i) = False
Next i
DoCmd.Close acForm, "SelectionForm", acSaveYes
End Sub
-------------------------
There must be another way! Can you imagine having to add four more combo boxes as selection criteria into that monster above?? Yikess!
ZaZa