Hi,
I can't figure out how to edit this module to process from two separate list boxes.
This module runs from selection(s) from a single list box.
I also need it to process selection(s) from another list box named "RepSelector" from the query "SalesRep"
Can someone help me?
Here's the working module for one list box.
Private Sub cmdRunReport_Click()
On Error GoTo Err_cmdRunReport_Click
Dim MyDB As Database
Dim qdf As QueryDef
Dim i As Integer, strSQL As String
Dim strWhere As String, strIN As String
Dim flgAll As Boolean
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM AR1_CustomerMaster"
'create the IN string by looping thru the listbox
For i = 0 To TerritorySelector.ListCount - 1
If TerritorySelector.Selected(i) Then
If TerritorySelector.Column(0, i) = " All" Then
flgAll = True
End If
strIN = strIN & "'" & TerritorySelector.Column(0, i) & "',"
End If
Next i
'create the WHERE string, stripping off the last comma of the IN string
strWhere = " WHERE [State]in (" & Left(strIN, Len(strIN) - 1) & ")"
'if "All" was selected, don't add the WHERE condition
If Not flgAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "Territory"
Set qdf = MyDB.CreateQueryDef("Territory", strSQL)
DoCmd.RunMacro "Process"
Exit_cmdRunReport_Click:
Exit Sub
Err_cmdRunReport_Click:
If Err.Number = 3265 Then '*** if the error is the query is missing
Resume Next '*** then skip the delete line and resume on the next line
ElseIf Err.Number = 5 Then
MsgBox "You must make at least one selection"
Resume Exit_cmdRunReport_Click
Else
MsgBox Err.Description '*** write out the error and exit the sub
Resume Exit_cmdRunReport_Click
End If
End Sub
I can't figure out how to edit this module to process from two separate list boxes.
This module runs from selection(s) from a single list box.
I also need it to process selection(s) from another list box named "RepSelector" from the query "SalesRep"
Can someone help me?
Here's the working module for one list box.
Private Sub cmdRunReport_Click()
On Error GoTo Err_cmdRunReport_Click
Dim MyDB As Database
Dim qdf As QueryDef
Dim i As Integer, strSQL As String
Dim strWhere As String, strIN As String
Dim flgAll As Boolean
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM AR1_CustomerMaster"
'create the IN string by looping thru the listbox
For i = 0 To TerritorySelector.ListCount - 1
If TerritorySelector.Selected(i) Then
If TerritorySelector.Column(0, i) = " All" Then
flgAll = True
End If
strIN = strIN & "'" & TerritorySelector.Column(0, i) & "',"
End If
Next i
'create the WHERE string, stripping off the last comma of the IN string
strWhere = " WHERE [State]in (" & Left(strIN, Len(strIN) - 1) & ")"
'if "All" was selected, don't add the WHERE condition
If Not flgAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "Territory"
Set qdf = MyDB.CreateQueryDef("Territory", strSQL)
DoCmd.RunMacro "Process"
Exit_cmdRunReport_Click:
Exit Sub
Err_cmdRunReport_Click:
If Err.Number = 3265 Then '*** if the error is the query is missing
Resume Next '*** then skip the delete line and resume on the next line
ElseIf Err.Number = 5 Then
MsgBox "You must make at least one selection"
Resume Exit_cmdRunReport_Click
Else
MsgBox Err.Description '*** write out the error and exit the sub
Resume Exit_cmdRunReport_Click
End If
End Sub