Produce a report from selected items in a listbox on a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Using Access 2002, I wish to produce a report. A user has a form with a
listbox, the listbox show items from a select query. The user then
multi-selects items in the listbox then prints these items in a report like
schedule.

Kindest regards
Duckula
 
It kind of depends on what the report looks like, and whether the user wants
a separate report for each of the selected items, or a single report that
has data for all of the selected items. It also depends on whether the
bound field in the listbox is numeric (an ID field would be best) or text.

If the user wants individual reports for each of the selected items
(easiest), then the following code should give you an idea of where to
start. Add a command button to the form (you'll need this for either
method).

Private Sub cmd_Report_Click()

Dim varItem as variant
Dim intBound as integer

intBound = me.listYourListName.BoundColumn
For Each varitem In Me.lstYourListName.ItemsSelected
DoCmd.OpenReport "yourReportName", acViewNormal, , "ID = " &
Me.lstYourListName.Column(intBound, varitem)
Next

End Sub

If, on the other hand, you want a single report that contains data for all
of the items selected, then you need to build a where clause that contains
the ID values for all of the selected fields. In that case, try something
like:

Private Sub cmd_Report_Click()

Dim varCriteria as variant
Dim varItem as variant
Dim intBound as integer

intBound = me.listYourListName.BoundColumn
varCriteria = NULL
For Each varitem In Me.lstYourListName.ItemsSelected
varCriteria = (varCriteria + ", ") &
me.lstYourListName.Column(intBound, varitem)
Next

IF ISNULL(varCriteria) then 'no items selected
' don't do anything, no items were selected
ELSE
strCriteria = "IN (" & varCriteria & ")"
DoCmd.OpenReport "yourReportName", acViewNormal, , "ID IN(" &
strCriteria & ")"
ENDIF

End Sub

If your bound column is text rather than numeric, then use something like
the following to concatenate the list of items selected in the listbox. The
chr$(34) represents the quotation mark, so this will wrap your text values
in quotes.

varCriteria = (varCriteria + ", ") & chr$(34) &
me.lstYourListName.Column(intBound, varitem) & chr$(34)

HTH
Dale
 
Dale thanks for that, its great. I had forgot about the ISNULL criteria,
again Dale thanks a lot

Kindest regards
Duckula
 
Back
Top