I am having some problems when using a forms criteria to run a query. I need to be able to select 1 or N names from the listbox and then select a beginning date and an ending date. The query should then be executed with the selection criteria listed above. Can anyone tell me what I am doing wrong here.
Here is what I have going:
1. Multi-select list box
2. Combo Box Beginning Month Range
3. Combo Box Ending Month Range
4. Control Button that run query based on the criteria selected (1-3)
Whats Working:
I can select 1 or N customer names from the listbox and it will run the query for all customers selected.
What are the problems
1. If I add the code to append the beginning date only
(will find records that are >= Beginning Month) and only
Select one customer name from the listbox the output
will be correct.
This one is successful:
Where = ((CustName = "Starbucks")) And ((posMonth >= 200602))
However if I select multiple customer names and the beginning date
the date range part of the query is ignored.
This one fails:
Where = ((CustName = "Starbucks") OR (CustName = "Sears")) And ((posMonth >= 200602))
2. If I select 1 or N customers from the listbox and select a beginning date and ending date. The date criteria seems to be ignored and all dates are listed.
this fails
Where = ((CustName = "Starbucks")) And ((posMonth >= 200602) OR (posMonth <= 200701))
The complete code for the Command Button is listed below.
Here is what I have going:
1. Multi-select list box
2. Combo Box Beginning Month Range
3. Combo Box Ending Month Range
4. Control Button that run query based on the criteria selected (1-3)
Whats Working:
I can select 1 or N customer names from the listbox and it will run the query for all customers selected.
What are the problems
1. If I add the code to append the beginning date only
(will find records that are >= Beginning Month) and only
Select one customer name from the listbox the output
will be correct.
Code:
sDate = "((DateSold >=" & Me!cmbMonthStart & ")"
sWhere = "(" & sWhere & ")" & " And " & sDate & ")"
DoCmd.OpenQuery "qry_GetRevenue2"
DoCmd.ApplyFilter , sWhere
This one is successful:
Where = ((CustName = "Starbucks")) And ((posMonth >= 200602))
However if I select multiple customer names and the beginning date
the date range part of the query is ignored.
This one fails:
Where = ((CustName = "Starbucks") OR (CustName = "Sears")) And ((posMonth >= 200602))
2. If I select 1 or N customers from the listbox and select a beginning date and ending date. The date criteria seems to be ignored and all dates are listed.
Code:
sDate = "((DateSold >=" & Me!cmbMonthStart & ")"
eDate = "(DateSold <=" & Me!cmbMonthEnd & "))"
sWhere = "(" & sWhere & ")" & " And " & sDate & " Or " & eDate
DoCmd.OpenQuery "qry_GetRevenue2"
DoCmd.ApplyFilter , sWhere
this fails
Where = ((CustName = "Starbucks")) And ((posMonth >= 200602) OR (posMonth <= 200701))
The complete code for the Command Button is listed below.
Code:
Dim sWhere As String ' Where condition
Dim lst As ListBox ' multiselect list box
Dim vItem As Variant ' items in listbox
Dim iLen As Integer ' length of string.
Dim sDate As String ' Beginning Month of Where Condition
Dim eDate As String ' Ending Month of Where Condition
Set lst = Me!List0
'loop through all items in listbox
For Each vItem In lst.ItemsSelected
If Not IsNull(vItem) Then
sWhere = sWhere & "(custNAME = """ & lst.ItemData(vItem) & """) OR "
End If
Next
iLen = Len(sWhere) - 4 ' Without trailing " OR ".
If iLen = 0 Then
sWhere = "(" & Left$(sWhere, iLen) & ")"
End If
sWhere = Left(sWhere, Len(sWhere) - 3)
sDate = "((DateSold >=" & Me!cmbMonthStart & ")"
eDate = "(DateSold <=" & Me!cmbMonthEnd & "))"
sWhere = "(" & sWhere & ")" & " And " & sDate & " Or " & eDate
MsgBox (sWhere)
DoCmd.OpenQuery "qry_GetRevenue2"
DoCmd.ApplyFilter , sWhere
Set lst = Nothing