report based on multi-select on form

Blackwidow

Registered User.
Local time
Today, 02:31
Joined
Apr 30, 2003
Messages
149
This is my first time doing this so bear with me, i have tried a couple examples in the database but keep getting stuck at the same point (my VB is not the best)

I want to run student reports for students selected through the list box, on highlighting them it puts their Admission Number into a field on the form called txtCriteria1 and then I click run report...

What should happen is that it will filter and run the report for each child selected... what it does is nothing :(

Private Sub Command4_Click()
'Preview Report


On Error GoTo Command4_Click_Error

If IsNull("Me.txtCriteria1") Or Me.txtCriteria1 = "" Then
Exit Sub
Else

DoCmd.OpenReport "rptMultipleStudentDataReport", acPreview
End If

On Error GoTo 0
Exit Sub

Command4_Click_Error:
If Err.Number = 2501 Then
Exit Sub
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Command4_Click of VBA Document Form_Multi-Select Listbox Example"
End If
End Sub

Private Sub List2_AfterUpdate()
Dim Criteria As String
Dim ctl As Control
Dim Itm As Variant

' Build a list of the selections.
Set ctl = Me.List2

For Each Itm In ctl.ItemsSelected
If Len(Criteria) = 0 Then
Criteria = ctl.ItemData(Itm)
Else
Criteria = Criteria & "," & ctl.ItemData(Itm)
End If
Next Itm
Me.txtCriteria1 = Criteria
End Sub

this is the code I have been using... do I need to alter the query the report is based on somehow?
 
You need to incorporate your criteria into the OpenReport command. All of the work can be done beneath your command button with only one piece of code. This code is typed on the fly so I can't guarantee anything... ;)

Code:
    Dim strList As String
    Dim i       As Integer

    [COLOR=DarkOliveGreen]'build the criteria from the list box selections[/COLOR]    
    For i = 0 To Me.List2.ListCount
       If Me.List2.Selected(i) Then
          strList = strList & Me.List2.ItemData(i) & ", "
       End If
    Next i

    If Len(strList) > 0 Then
         strList = Left$(strList, Len(strList) - 2)
         DoCmd.OpenReport "rptMultipleStudentDataReport", acPreview, , "AdmissionNumber IN (" & strList & ")"
    End If
 

Users who are viewing this thread

Back
Top Bottom