You shouldn't have any code in the list box's event procedure, just in the
button's. The following is the code from the module of a form of mine which
does the same sort of thing:
Option Compare Database
Option Explicit
Private Sub cmdPreview_Click()
Dim varItem As Variant
Dim strStkNumbersList As String
Dim strCriteria As String
Dim ctrl As Control
Set ctrl = Me.lstStkNumbers
' loop through list box's ItemsSelected collection
' and build comma separated list of selected items
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strStkNumbersList = strStkNumbersList & "," & ctrl.ItemData
(varItem)
Next varItem
' remove leading comma
strStkNumbersList = Mid(strStkNumbersList, 2)
strCriteria = "AddressID In(" & strStkNumbersList & ")"
DoCmd.OpenReport "rptStock", _
View:=acViewPreview, _
WhereCondition:=strCriteria
Else
MsgBox "No Stock Numbers Selected", vbInformation, "Warning"
End If
End Sub
As you see the code is in the button's Click event procedure. If in your
form you have the code in the button's event like this AND in the list box's
Click event then just delete everything in the latter from Private Sub
lstContractors_Click() right down to End Sub just as you'd delete text in a
document. If the code isn't yet in the button's Click event then, before
deleting the code first copy everything between Private Sub
lstContractors_Click() and End Sub to the clipboard, then create the Click
event procedure for the button from its properties sheet in the usual way and
paste the code into that procedure. You can then delete the original list
box's event procedure.
Ken Sheridan
Stafford, England
Thank you really appreciate all your help.
I cut the code out of the event procedure in the list box and created a
command button called preview. It still is not working - see code below: It
looks like it is still in the list box even. How do I delete the code for?
Private Sub lstContractors_Click()
Dim varItem As Variant
Dim strIDList As String
Dim strCriteria As String
Dim ctrl As Control
Set ctrl = Me.lstContractors
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strIDList = strIDList & "," & ctrl.ItemData(varItem)
Next varItem
' remove leading comma
strIDList = Mid(strIDList, 2)
strCriteria = "ID In(" & strIDList & ")"
DoCmd.OpenReport "rptContractors", _
View:=acViewPreview, _
WhereCondition:=strCriteria
Else
MsgBox "No Contractors selected", vbInformation, "Warning"
End If
End Sub
It sounds like you've put the code in the list box's AfterUpdate event
procedure. Cut it from there and paste it into the Click event procedure of
[quoted text clipped - 16 lines]
mulit-select the companies I want the report to print. I was able to create a
list box allowing for multi-criteria select but how do I get it to a report?
--
.