listbox report error

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

Guest

I have a report that is generated from names in a listbox, it runs fine but
it always gives me a name at the end of the report that I did not select from
the listbox. What could be wrong. Thanks
 
I have a report that is generated from names in a listbox, it runs fine but
it always gives me a name at the end of the report that I did not select from
the listbox. What could be wrong. Thanks

What could be wrong is the way you've set up the report. Since you
don't say how you did so it's more than a bit difficult to say!

How is the report "generated"? What is its Recordsource? Could you
post the code which launches the report?

John W. Vinson[MVP]
 
The report is generated by a query here is what it looks like: query is
"allemployee"
Private Sub CmdPreview_Click()
Dim v As Variant
Dim Frm As Form
Dim ctl As Control
Dim theId As Long
Dim WhereCrit As String

If Me.ByName.ItemsSelected.Count = 0 Then
MsgBox "Please select a Name or two.", vbExclamation, "No Name Selected"
'and then scram.
Exit Sub
End If

Set Frm = Forms!EmployeeByName
Set ctl = Frm!ByName

WhereCrit = "RecordID = "

For Each v In ctl.ItemsSelected
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Coordinates: 1st column (0); row v
'where v changes for each round of the loop.
theId = ctl.Column(0, v)
'Tag on to string.
WhereCrit = WhereCrit & theId & " OR RecordID = "
Next v

WhereCrit = left(WhereCrit, Len(WhereCrit) - 17)
DoCmd.OpenReport "ByNameReport", acViewPreview, , WhereCrit

End Sub

Private Sub Form_Load()

'Fill the listbox using a saved query.

Me.ByName.RowSource = "AllEmployees"

End Sub
 
The report is generated by a query here is what it looks like: query is
"allemployee"

I'd suggest stepping through the code in debug mode, and displaying
the WhereCrit string. If you copy and paste WhereCrit onto the
Criteria line of the query in design view, and then open the query, do
you get the expected results?

Note that you can do this a bit more compactly using the IN() clause:

WhereCrit = "RecordID IN("

For Each v In ctl.ItemsSelected
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Coordinates: 1st column (0); row v
'where v changes for each round of the loop.
theId = ctl.Column(0, v)
'Tag on to string.
WhereCrit = WhereCrit & theId & ","
Next v

WhereCrit = left(WhereCrit, Len(WhereCrit) - 1) & ")"


John W. Vinson[MVP]
 
Thanks A lot, that did it, I replace the string with your and it remove the
name in the report. I still don't quite understand what it did but again
thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Info on Report 3
Listbox problem 3
Access Cannot select items in listbox 1
Open Report with Criteria From Form 6
Control using a Listbox 7
Print values of a listbox 2
List of Reports and Forms 2
Deselecting items in a listbox 2

Back
Top