Hi,
I am a novice to VB, using Access 2003 on a XP-SP2 platform, and I have a small database for tracking expenses, and want to be able to filter my expense report by date. I currently have a form with two list boxes, purchase and payment type, and then two date fields txtStartDate and txtEndDate. Filter works fine when no dates are chosen, problem is with my date picker - if I choose dates I get a "syntax error (missing operator)" error in my strWhere statement (I have used the debugger, but can't pinpoint the syntax error.. I have posted the script for the "Apply FIlter" command, any assistance would be appreciated. By the way, my date field in my source table us uDate, and in my report is "repDate" to avoid reserved fields.
Script:
I am a novice to VB, using Access 2003 on a XP-SP2 platform, and I have a small database for tracking expenses, and want to be able to filter my expense report by date. I currently have a form with two list boxes, purchase and payment type, and then two date fields txtStartDate and txtEndDate. Filter works fine when no dates are chosen, problem is with my date picker - if I choose dates I get a "syntax error (missing operator)" error in my strWhere statement (I have used the debugger, but can't pinpoint the syntax error.. I have posted the script for the "Apply FIlter" command, any assistance would be appreciated. By the way, my date field in my source table us uDate, and in my report is "repDate" to avoid reserved fields.
Script:
Code:
Private Sub cmdApplyFilter_Click()
Dim varItem As Variant
Dim strCategory As String
Dim strPayment As String
Dim strFilter As String
Dim strReport As String 'Name of report to open.
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.
' Check that the report is open
If SysCmd(acSysCmdGetObjectState, acReport, "repExpense") <> acObjStateOpen Then
MsgBox "You must open the report first."
Exit Sub
End If
' Build criteria string from lstCategory listbox
For Each varItem In Me.lstCategory.ItemsSelected
strCategory = strCategory & ",'" & Me.lstCategory.ItemData(varItem) _
& "'"
Next varItem
If Len(strCategory) = 0 Then
strCategory = "Like '*'"
Else
strCategory = Right(strCategory, Len(strCategory) - 1)
strCategory = "IN(" & strCategory & ")"
End If
' Build criteria string from lstPayment listbox
For Each varItem In Me.lstPayment.ItemsSelected
strPayment = strPayment & ",'" & Me.lstPayment.ItemData(varItem) _
& "'"
Next varItem
If Len(strPayment) = 0 Then
strPayment = "Like '*'"
Else
strPayment = Right(strPayment, Len(strPayment) - 1)
strPayment = "IN(" & strPayment & ")"
End If
' Build criteria string for date
strReport = "repExpense"
strField = "Date"
If IsNull(Me.txtStartDate) Then
If Not IsNull(Me.txtEndDate) Then 'End date, but no start.
strWhere = strField & "<=#" & Me.txtEndDate
End If
Else
If IsNull(Me.txtEndDate) Then 'Start date, but no End.
strWhere = strField & ">=#" & Me.txtStartDate
Else 'Both start and end dates.
strWhere = strField & " Between #" & Me.txtStartDate _
& "# And #" & Me.txtEndDate & "#"
End If
End If
Debug.Print strWhere 'For debugging purposes only.
DoCmd.OpenReport strReport, acViewPreview, , strWhere
' Build filter string
strFilter = "[Category] " & strCategory & _
" AND [PmtType] " & strPayment & _
" AND [uDate] " & strWhere
' Apply the filter and switch it on
With Reports![repExpense]
.Filter = strFilter
.FilterOn = True
End With
End Sub
Comment