J
jdub
Hey Forum,
I'm a newbie to access and have been tasked to allow users to select a group
of names from a list and display them in a report. I've created a query that
grabs the names from the database. I've created a form with a list box and
I've created a report. I'm able to select the names and then click the
button to start the report. My report comes in blank. I'm not sure what
Control Source to use for the field. I've searched the internet and have
found some code that I'm using. It is below. The report name is GameRoster,
the Listbox is List0 and the field I need is the Name field that is being
returned in the query for the listbox.
Any help would be greatly appreciated.
Private Sub ShowRecord_Click()
On Error GoTo Err_Handler
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
strDelim = """"
strDoc = "GameRoster"
'Loop through the ItemsSelected in the list box.
With Me.List0
For Each varItem In List0.ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & List0.ItemData(varItem) &
strDelim & ","
'Build up the description from the text in the visible column.
See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Name] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Player Name " & Left$(strDescrip, lngLen)
End If
End If
'Report will not filter if open, so close it.
If CurrentProject.AllReports(strDoc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If
'Omit the last argument for Access 2000 and earlier. See note 4.
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere,
OpenArgs:=strDescrip
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"ShowRecord_Click"
End If
Resume Exit_Handler
End Sub
I'm a newbie to access and have been tasked to allow users to select a group
of names from a list and display them in a report. I've created a query that
grabs the names from the database. I've created a form with a list box and
I've created a report. I'm able to select the names and then click the
button to start the report. My report comes in blank. I'm not sure what
Control Source to use for the field. I've searched the internet and have
found some code that I'm using. It is below. The report name is GameRoster,
the Listbox is List0 and the field I need is the Name field that is being
returned in the query for the listbox.
Any help would be greatly appreciated.
Private Sub ShowRecord_Click()
On Error GoTo Err_Handler
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
strDelim = """"
strDoc = "GameRoster"
'Loop through the ItemsSelected in the list box.
With Me.List0
For Each varItem In List0.ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & List0.ItemData(varItem) &
strDelim & ","
'Build up the description from the text in the visible column.
See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Name] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Player Name " & Left$(strDescrip, lngLen)
End If
End If
'Report will not filter if open, so close it.
If CurrentProject.AllReports(strDoc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If
'Omit the last argument for Access 2000 and earlier. See note 4.
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere,
OpenArgs:=strDescrip
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"ShowRecord_Click"
End If
Resume Exit_Handler
End Sub