I am using Access 2007 and I have one table with numerous fields. Two of these fields allow multiple values that represent selected criteria by the user. These criteria are entered via a form with combo boxes where the criteria are hard coded in the ROW source. The user is limited to the options presented (non editable list). By way of example, one field is called Color and another field is called Shape.
Color options are: Red, Green, Blue, Orange, Yellow, Black
Shape options are: Circle, Square, Triangle, Hexagon, Pentagon
The user may select one or more (or none) of these options so the content of these two fields in may have one, multiple or null entries.
For reporting purposes I need to allow a user to generate a report that lists all records according to a query of these two fields. For example, out of 1000 records, I need to find all records where the field Color has red and/or yellow recorded and Shape has square, Triangle and pentagon recorded. Or the user may select some other combination from the two where one or more criteria are selected.
I have been trying to do this by using two list boxes in a form where each list box shows all possible options for each field and the user would select one or more of the options from each list boxes. Then the user clicks on a button that sends the corresponding records to a report.
I have successfully developed one list box in a form that allows for multiple selections of Color. When the user clicks on a view report button, the form closes and a report is opened listing all records that have red in the color field. I need to add a second list box for Shape so my users can select one or more colors and one or more shapes to generate a report showing records where the selected criteria apply.
The code shown below for my one working list box comes from another forum. I am not really a VB programmer, though I am comfortable working with VB if I have sufficient guidance.
My table is called Products.
My two fields in question are called Color and Shape.
My form is called frmProducts and I have my reference tag set to rptProducts.
My list box , called lsColorReport, has Row Source to define the colors that need to be listed.
My command button called cmdReport uses this sniped of VB code OnClick:
The Call MultipleValueCriteria code is:
Can anyone suggest how I need to modify this code and/or my form structure so that I can generate a filtered report based on the multi select criteria of two list boxes, one for Color and one for Shape?
Thanks.
Color options are: Red, Green, Blue, Orange, Yellow, Black
Shape options are: Circle, Square, Triangle, Hexagon, Pentagon
The user may select one or more (or none) of these options so the content of these two fields in may have one, multiple or null entries.
For reporting purposes I need to allow a user to generate a report that lists all records according to a query of these two fields. For example, out of 1000 records, I need to find all records where the field Color has red and/or yellow recorded and Shape has square, Triangle and pentagon recorded. Or the user may select some other combination from the two where one or more criteria are selected.
I have been trying to do this by using two list boxes in a form where each list box shows all possible options for each field and the user would select one or more of the options from each list boxes. Then the user clicks on a button that sends the corresponding records to a report.
I have successfully developed one list box in a form that allows for multiple selections of Color. When the user clicks on a view report button, the form closes and a report is opened listing all records that have red in the color field. I need to add a second list box for Shape so my users can select one or more colors and one or more shapes to generate a report showing records where the selected criteria apply.
The code shown below for my one working list box comes from another forum. I am not really a VB programmer, though I am comfortable working with VB if I have sufficient guidance.
My table is called Products.
My two fields in question are called Color and Shape.
My form is called frmProducts and I have my reference tag set to rptProducts.
My list box , called lsColorReport, has Row Source to define the colors that need to be listed.
My command button called cmdReport uses this sniped of VB code OnClick:
Code:
Private Sub cmdReport_Click()
Call MultipleValueCriteria(Me, _
Me!lsColorReport, "[Products.Color.Value]")
End Sub
The Call MultipleValueCriteria code is:
Code:
Function MultipleValueCriteria(pform As Form, _
pcontrol As ListBox, pfield As String)
'Launch rptCustomers using
'Or criteria built on field
'passed by pfield; report must be
'passed via the form's Tag property.
Dim var As Variant
Dim strCriteria As String
If pcontrol.ItemsSelected.Count = 0 Then
MsgBox "Please select a city.", _
vbOKOnly, "Error"
Exit Function
'Build a SQL statement using
'selected cities.
Else
'Criteria expression uses literal string
'values. If using numeric or date values,
'update delimiter component.
For Each var In pcontrol.ItemsSelected
strCriteria = strCriteria & _
pfield & " = '" & _
pcontrol.ItemData(var) _
& "' Or "
Next var
End If
strCriteria = Left(strCriteria, _
Len(strCriteria) - 4)
Debug.Print strCriteria
'Open filtered report and close form.
DoCmd.OpenReport pform.Tag, _
acViewPreview, , strCriteria
DoCmd.Close acForm, pform.Name
Set pcontrol = Nothing
End Function
Can anyone suggest how I need to modify this code and/or my form structure so that I can generate a filtered report based on the multi select criteria of two list boxes, one for Color and one for Shape?
Thanks.