I have on form frame with 3 options to select report type (All, Top
20, Selected) where Selected reveals ListBox with multi-selection.
There are many options selectable and when combined it will produce
around 7,800 different reports. My goal is to use minimum set of
queries and reports and I will cover with only couple of queries and
reports all cases. Therefore high optimization is required.
Using your solution and for selection from ListBox I have in query:
Expr1: InStr(GetTitles(),[tblTitles].[NameVO])
Criteria: Between (IIf([forms]![frmReport]![frame85]=3,1,0)) And
(IIf([forms]![frmReport]![frame85]=3,1000000,1000000))
"[frame85]=3" indicates that user have selected ListBox option.
Note that I have used workaround - Between 1 and 1000000 or Between 1
and 1000000 and I am not happy with this solution, but at the moment I
don't have any better. Basically this is my question - are there any
better solution?
GetTitles() is sub which determine report type selected and provides
filter to query.
As long as you have a button on the form that open the
report, you can use code instead of going through
contortions in the query.
Use the OpenReport method's WhereCondition argument to
filter the data to the items in the list box, something like
this air code:
Select Case Me.frame85
Case 3
stWhere = "NameVO IN(" & GetTitles() & ")"
DoCmd.OpenReport "report name", acViewPreview, _
WhereCondition:= stWhere
Case 1
DoCmd.OpenReport "report name", acViewPreview
Case 2
DoCmd.OpenReport "report name", acViewPreview, _
OpenArgs:= "XXX"
End Select
To deal with case 2, you will need some code in the report's
Open event. For example:
If Not IsNull(Me.OpenArgs) Then
Me.RecordSource = "your top 20 query name"
End If
You will probably have to tweak the GetTitles function to
use a comma separator and if the NameVO field is a Text
field, add quote delimiters around each item.
Using these techniques to deal with your options, you only
need one copy of the report. You will need two queries,
both of them without any criteria. Just basic Select
queries, one with the TOP 20 and associated ORDER BY clause.
Thanks Marsh,
But I have made query as record source for report already. That is
what GetTitles() sub do too - it determines which option have been
selected and provide to query string like: "Star Wars" OR "Mr. Bean"
OR... and simply filters query for all 3 cases. I am copying that Sub
here hopping that someone will find useful this approach:
Public Sub GetTitles()
'Used to determine which report type is selected
'frame85 is a frame control with 3 options
Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Dim strTitles As String
Dim rst As Recordset, qdf As QueryDef
Set frm = Forms!frmReport 'Name of form to define report criteria
Set ctl = frm!lstTitles 'ListBox with multi-selection
If frm!Frame85 = 3 Then 'Get selected titles option selected
For Each varItem In ctl.ItemsSelected 'Loops through ListBox
strTitles = strTitles & ctl.ItemData(varItem) & " OR "
Next varItem
GetTitles = Left(strTitles, Len(strTitles) - 4) 'Removes last
" OR " characters
ElseIf frm!Frame85 = 2 Then 'Get Top 20 Titles option selected
Set qdf = CurrentDb.QueryDefs("qryTop20Titles")
qdf.Parameters(0) = [Forms]![frmReport]![fldDateFrom] 'Dates
are optional
qdf.Parameters(1) = [Forms]![frmReport]![fldDateTo]
Set rst = qdf.OpenRecordset
With rst
Do While .EOF = False
strTitles = strTitles & !NameVO & " OR " 'NameVO =
Title Name
.MoveNext
Loop
End With
Set rst = Nothing
GetTitles = Left(strTitles, Len(strTitles) - 4) 'Removes last
" OR " characters
Else 'Get ALL Titles 'Else 3rd option have been selected
GetTitles = "((tblTheatresTitlesAdmissionsShares.IDTitle) >
0)"
End If
End Sub
Basically, as you see, I have used your idea, but the question is this
Expr1: InStr(GetTitles(),[tblTitles].[NameVO])
Criteria: Between (IIf([forms]![frmReport]![frame85]=3,1,0)) And
(IIf([forms]![frmReport]![frame85]=3,1000000,1000000))
the best solution?
Can I wrote something like
Between (1) and (Unlimited) as Like(IIf..., >0, >-1) won't work?
Actually is there any way to have filter as ">0" or ">-1" (for all)? I
assume "Like" returns string so >-1 is not equal with ">-1".
Couple of notes: Select Case is better approach but when I have
started I have had 2 options only. Second, I have left "frame85" there
instead something more meaningful so it looks obvious that I have used
frame. Last, qryTop20 provides list of Top 20 Titles by Gross BO.
Finally, single query solution covers couple of hundreds of different
reports as there are more filtered fields in query like the one above
and it works really quick.
Best regards,
Branislav Mihaljev