R
ryguy7272
How can I filter by Dates on a Form?
I have this code behind a Form named ‘ReportForm’:
Option Compare Database
Option Explicit
Private Sub cmdRun_Click()
Dim varItem As Variant
Dim strCust As String
Dim strFilter As String
' Check that the report is open
DoCmd.OpenReport "rptFilter", acViewPreview
' If SysCmd(acSysCmdGetObjectState, acReport, "rptFilter") <>
acObjStateOpen Then
' MsgBox "You must open the report first."
' Exit Sub
' End If
' Build criteria string from lstCust listbox
For Each varItem In Me.lstCust.ItemsSelected
strCust = strCust & ",'" & Me.lstCust.ItemData(varItem) _
& "'"
Next varItem
If Len(strCust) = 0 Then
strCust = "Like '*'"
Else
strCust = Right(strCust, Len(strCust) - 1)
strCust = "IN(" & strCust & ")"
End If
' Build filter string
strFilter = "[Cust] " & strCust
' Apply the filter and switch it on
With Reports![rptFilter]
.Filter = strFilter
.FilterOn = True
End With
End Sub
Private Sub cmdRemoveFilter_Click()
On Error Resume Next
' Switch the filter off
Reports![rptFilter].FilterOn = False
End Sub
The code works great, and allows me to filter the results on the Form by
passing in a variable named ‘strCust’, for Customer. What I’m trying to do
now, is pass dates to the Report, so I can filter by both Dates and Customer.
I want to choose the records BETWEEN the cboFrom date and the cboTo date.
On the Form, I have two ComboBoxes, one named ‘cboFrom’ and the other is
named ‘cboTo’ (both are bound to a Table). How can I modify my VBA so I can
filter by both Dates and Customer?
Thanks so much!
Ryan--
I have this code behind a Form named ‘ReportForm’:
Option Compare Database
Option Explicit
Private Sub cmdRun_Click()
Dim varItem As Variant
Dim strCust As String
Dim strFilter As String
' Check that the report is open
DoCmd.OpenReport "rptFilter", acViewPreview
' If SysCmd(acSysCmdGetObjectState, acReport, "rptFilter") <>
acObjStateOpen Then
' MsgBox "You must open the report first."
' Exit Sub
' End If
' Build criteria string from lstCust listbox
For Each varItem In Me.lstCust.ItemsSelected
strCust = strCust & ",'" & Me.lstCust.ItemData(varItem) _
& "'"
Next varItem
If Len(strCust) = 0 Then
strCust = "Like '*'"
Else
strCust = Right(strCust, Len(strCust) - 1)
strCust = "IN(" & strCust & ")"
End If
' Build filter string
strFilter = "[Cust] " & strCust
' Apply the filter and switch it on
With Reports![rptFilter]
.Filter = strFilter
.FilterOn = True
End With
End Sub
Private Sub cmdRemoveFilter_Click()
On Error Resume Next
' Switch the filter off
Reports![rptFilter].FilterOn = False
End Sub
The code works great, and allows me to filter the results on the Form by
passing in a variable named ‘strCust’, for Customer. What I’m trying to do
now, is pass dates to the Report, so I can filter by both Dates and Customer.
I want to choose the records BETWEEN the cboFrom date and the cboTo date.
On the Form, I have two ComboBoxes, one named ‘cboFrom’ and the other is
named ‘cboTo’ (both are bound to a Table). How can I modify my VBA so I can
filter by both Dates and Customer?
Thanks so much!
Ryan--