G
Guest
I am using a list box to identify the criteria for a query. Please help:
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim strIO As String
Dim strIP As String
Dim strIQ As String
Dim strIR As String
Dim strIS As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
dtDate1 = Forms!Activity_View_frm!Date1
dtDate2 = Forms!Activity_View_frm!Date2
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM Activity_tbl"
'Build the IN string by looping through the listbox
For i = 0 To CustomerList.ListCount - 1
If CustomerList.Selected(i) Then
If CustomerList.Column(0, i) = "*All Customers" Then
flgSelectAll = True
End If
strIN = strIN & "'" & CustomerList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To ZoneList.ListCount - 1
If ZoneList.Selected(i) Then
If ZoneList.Column(0, i) = "*All" Then
flgSelectAll = True
End If
strIO = strIO & "'" & ZoneList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To AccountTypeList.ListCount - 1
If AccountTypeList.Selected(i) Then
If AccountTypeList.Column(0, i) = "*All" Then
flgSelectAll = True
End If
strIP = strIP & "'" & AccountTypeList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To DepartmentList.ListCount - 1
If DepartmentList.Selected(i) Then
If DepartmentList.Column(0, i) = "*All Departments" Then
flgSelectAll = True
End If
strIQ = strIQ & "'" & DepartmentList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To SalesPitchList.ListCount - 1
If SalesPitchList.Selected(i) Then
If SalesPitchList.Column(0, i) = "*All Sales Pitches" Then
flgSelectAll = True
End If
strIR = strIR & "'" & SalesPitchList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To ActivityList.ListCount - 1
If ActivityList.Selected(i) Then
If ActivityList.Column(0, i) = "*All Activities" Then
flgSelectAll = True
End If
strIS = strIS & "'" & ActivityList.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [Customer Name] in (" & Left(strIN, Len(strIN) - 1) &
") and [Zone] in (" & Left(strIO, Len(strIO) - 1) & ") and [Account Type] in
(" & Left(strIP, Len(strIP) - 1) & ") and [Department] in (" & Left(strIQ,
Len(strIQ) - 1) & ") and [Business Type] in (" & Left(strIR, Len(strIR) - 1)
& ") and [Activity] in (" & Left(strIS, Len(strIS) - 1) & ") and [Date]
Between #" & dtDate1 & "# And #" & dtDate2 & "#"
'If "*All Customers" was selected in the listbox, don't add the WHERE
condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryActivity"
Set qdef = MyDB.CreateQueryDef("qryActivity", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryActivity", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.CustomerList.ItemsSelected
Me.CustomerList.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", , "Selection
Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim strIO As String
Dim strIP As String
Dim strIQ As String
Dim strIR As String
Dim strIS As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
dtDate1 = Forms!Activity_View_frm!Date1
dtDate2 = Forms!Activity_View_frm!Date2
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM Activity_tbl"
'Build the IN string by looping through the listbox
For i = 0 To CustomerList.ListCount - 1
If CustomerList.Selected(i) Then
If CustomerList.Column(0, i) = "*All Customers" Then
flgSelectAll = True
End If
strIN = strIN & "'" & CustomerList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To ZoneList.ListCount - 1
If ZoneList.Selected(i) Then
If ZoneList.Column(0, i) = "*All" Then
flgSelectAll = True
End If
strIO = strIO & "'" & ZoneList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To AccountTypeList.ListCount - 1
If AccountTypeList.Selected(i) Then
If AccountTypeList.Column(0, i) = "*All" Then
flgSelectAll = True
End If
strIP = strIP & "'" & AccountTypeList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To DepartmentList.ListCount - 1
If DepartmentList.Selected(i) Then
If DepartmentList.Column(0, i) = "*All Departments" Then
flgSelectAll = True
End If
strIQ = strIQ & "'" & DepartmentList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To SalesPitchList.ListCount - 1
If SalesPitchList.Selected(i) Then
If SalesPitchList.Column(0, i) = "*All Sales Pitches" Then
flgSelectAll = True
End If
strIR = strIR & "'" & SalesPitchList.Column(0, i) & "',"
End If
Next i
'Build the IN string by looping through the listbox
For i = 0 To ActivityList.ListCount - 1
If ActivityList.Selected(i) Then
If ActivityList.Column(0, i) = "*All Activities" Then
flgSelectAll = True
End If
strIS = strIS & "'" & ActivityList.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [Customer Name] in (" & Left(strIN, Len(strIN) - 1) &
") and [Zone] in (" & Left(strIO, Len(strIO) - 1) & ") and [Account Type] in
(" & Left(strIP, Len(strIP) - 1) & ") and [Department] in (" & Left(strIQ,
Len(strIQ) - 1) & ") and [Business Type] in (" & Left(strIR, Len(strIR) - 1)
& ") and [Activity] in (" & Left(strIS, Len(strIS) - 1) & ") and [Date]
Between #" & dtDate1 & "# And #" & dtDate2 & "#"
'If "*All Customers" was selected in the listbox, don't add the WHERE
condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryActivity"
Set qdef = MyDB.CreateQueryDef("qryActivity", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryActivity", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.CustomerList.ItemsSelected
Me.CustomerList.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", , "Selection
Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub