Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variable to a Report 2

Status
Not open for further replies.

jdegeorge

Programmer
Mar 19, 2002
1,313
US
Hi

I have a report that I want to filter based on user input.

I found this FAQ from Randy Smith ( which got me on my way and works well with only 1 list box, but it's now getting complicated because I need several list boxes.

Here's the code so far, with only 2 variables:

[tt]
Private Sub btnPrintReport_Click()

Dim strFilter As String
Dim varItem As Variant

' loop through Area/Function listbox items selected
For Each varItem In Me!lstAreaFunction.ItemsSelected
strFilter = strFilter & "[Area/Function] = " & _
Me![lstAreaFunction].ItemData(varItem) & " OR "
Next ' continue loop

' the next bit of code will subtract out the last "OR"
If strFilter <> &quot;&quot; Then
strFilter = Left(strFilter, Len(strFilter) - 4)
End If

If IsNull(strFilter) Then
strFilter = &quot;&quot;

' loop through Category listbox items selected
For Each varItem In Me!lstCategory.ItemsSelected
strFilter = strFilter & &quot;[Category] = &quot; & _
Me![lstCategory].ItemData(varItem) & &quot; OR &quot;
Next ' continue loop

' the next bit of code will subtract out the last &quot;OR&quot;
If strFilter <> &quot;&quot; Then
strFilter = Left(strFilter, Len(strFilter) - 4)
End If
Else
'Add &quot;AND&quot; to strFilter
strFilter = strFilter & &quot; And &quot;

' loop through Category listbox items selected
For Each varItem In Me!lstCategory.ItemsSelected
strFilter = strFilter & &quot;[Category] = &quot; & _
Me![lstCategory].ItemData(varItem) & &quot; OR &quot;
Next ' continue loop

' the next bit of code will subtract out the last &quot;OR&quot;
If strFilter <> &quot;&quot; Then
strFilter = Left(strFilter, Len(strFilter) - 4)
End If
End If

DoCmd.OpenReport &quot;Area/Function&quot;, acPreview, , strFilter

End Sub[/tt]


The problem with this is that I have a number of filters that are optional. At this rate, I'll have nested If statements up the wazoo and the coding will get very ugly.

Does anyone know of an easy way to have several multi-select list boxes on a form and to pass those values to a report filter? Thanks in advance.

Jim DeGeorge [wavey]
 
dhookom

Thought you'd enjoy a laugh. When you said that the format of your tag was &quot;lbl + T or N or D + Field Name&quot; you really meant FIELD NAME. I was using a variation of the field name in naming my lbo control and it didn't work. I had to rename a few controls, but it works now. Thanks again!

Jim DeGeorge [wavey]
 
Glad it works for you. I'll consider making this available in the FAQs. Every application should have a bunch of this stuff for generic use.

Duane
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top