A
Alex
I really need help with this. I've posted a few questions, but not having
any luck. I have a report that is filtered by an input form that contains a
multi-select list box. Normally I would use a query to filter the data then
use that query as the report data source. Since I need users to be able to
select more than one choice in a list box, I can't use the query. My user
input form contains a button with the following code that works great to
filter the report.
Here's my problem: I have a chart that I need to put in the same report.
When I run the report, the chart is not filtered the same as the report. How
can I use vba to filter my chart? Is it as simple as changing ' Apply the
filter and switch it on With Reports![LocationsReportbyStorage].Filter =
strFilter.FilterOn = True End With End Sub? I'm totally stuck on this and
will appreciate any help you can give. I'm very new to vba. Thank you.
Private Sub cmdApplyFilter_Click()
Dim VarItem As Variant
Dim strStore As String
Dim strLocation As String
Dim datBeginDate As Date
Dim datEndDate As Date
Dim strFilter As String
' Check for Location
If Len(Me.cmdLocation.Value & "") = 0 Then
MsgBox "You must pick a location"
Exit Sub
End If
'Check for Beginning and Ending date
If Len(Me.cmdBeginDate.Value & "") = 0 Then
MsgBox "You must type a Beginning date"
Exit Sub
End If
If Len(Me.cmdEndDate.Value & "") = 0 Then
MsgBox "You must type an Ending date"
Exit Sub
End If
' Open Report
If SysCmd(acSysCmdGetObjectState, acReport, "LocationsReportbyStorage")
<> acObjStateOpen Then
DoCmd.OpenReport "LocationsReportbyStorage", acViewPreview
End If
' Build criteria string from StoreRoom listbox
For Each VarItem In Me.cmdStoreRoom.ItemsSelected
strStore = strStore & ",'" & Me.cmdStoreRoom.ItemData(VarItem) _
& "'"
Next VarItem
If Len(strStore) = 0 Then
strStore = "Like '*'"
Else
strStore = Right(strStore, Len(strStore) - 1)
strStore = "IN(" & strStore & ")"
End If
' Build criteria string from Location option group
Select Case Me.cmdLocation.Value
Case 1
strLocation = "='1'"
Case 2
strLocation = "='2'"
End Select
'Build Beginning & Ending date parameter
If Not IsNull(Me.cmdBeginDate) Then
datBeginDate = Me.cmdBeginDate
End If
If Not IsNull(Me.cmdEndDate) Then
datEndDate = Me.cmdEndDate
End If
' Build filter string
strFilter = "[StorageRoom] " & strStore & " AND [Location] " &
strLocation & " AND [Date] Between #" & datBeginDate & "# and #" & datEndDate
& "#"
' Apply the filter and switch it on
With Reports![LocationsReportbyStorage]
.Filter = strFilter
.FilterOn = True
End With
End Sub
any luck. I have a report that is filtered by an input form that contains a
multi-select list box. Normally I would use a query to filter the data then
use that query as the report data source. Since I need users to be able to
select more than one choice in a list box, I can't use the query. My user
input form contains a button with the following code that works great to
filter the report.
Here's my problem: I have a chart that I need to put in the same report.
When I run the report, the chart is not filtered the same as the report. How
can I use vba to filter my chart? Is it as simple as changing ' Apply the
filter and switch it on With Reports![LocationsReportbyStorage].Filter =
strFilter.FilterOn = True End With End Sub? I'm totally stuck on this and
will appreciate any help you can give. I'm very new to vba. Thank you.
Private Sub cmdApplyFilter_Click()
Dim VarItem As Variant
Dim strStore As String
Dim strLocation As String
Dim datBeginDate As Date
Dim datEndDate As Date
Dim strFilter As String
' Check for Location
If Len(Me.cmdLocation.Value & "") = 0 Then
MsgBox "You must pick a location"
Exit Sub
End If
'Check for Beginning and Ending date
If Len(Me.cmdBeginDate.Value & "") = 0 Then
MsgBox "You must type a Beginning date"
Exit Sub
End If
If Len(Me.cmdEndDate.Value & "") = 0 Then
MsgBox "You must type an Ending date"
Exit Sub
End If
' Open Report
If SysCmd(acSysCmdGetObjectState, acReport, "LocationsReportbyStorage")
<> acObjStateOpen Then
DoCmd.OpenReport "LocationsReportbyStorage", acViewPreview
End If
' Build criteria string from StoreRoom listbox
For Each VarItem In Me.cmdStoreRoom.ItemsSelected
strStore = strStore & ",'" & Me.cmdStoreRoom.ItemData(VarItem) _
& "'"
Next VarItem
If Len(strStore) = 0 Then
strStore = "Like '*'"
Else
strStore = Right(strStore, Len(strStore) - 1)
strStore = "IN(" & strStore & ")"
End If
' Build criteria string from Location option group
Select Case Me.cmdLocation.Value
Case 1
strLocation = "='1'"
Case 2
strLocation = "='2'"
End Select
'Build Beginning & Ending date parameter
If Not IsNull(Me.cmdBeginDate) Then
datBeginDate = Me.cmdBeginDate
End If
If Not IsNull(Me.cmdEndDate) Then
datEndDate = Me.cmdEndDate
End If
' Build filter string
strFilter = "[StorageRoom] " & strStore & " AND [Location] " &
strLocation & " AND [Date] Between #" & datBeginDate & "# and #" & datEndDate
& "#"
' Apply the filter and switch it on
With Reports![LocationsReportbyStorage]
.Filter = strFilter
.FilterOn = True
End With
End Sub