Solved

Multifilter for Report

Posted on 2008-10-27
4
272 Views
Last Modified: 2013-11-28
Hi Experts,

Thank you for your time and patience.

I am setting up a form with cascading combos to act as a filter for queries / reports.
On there form there are a number of cascading combos and four command buttons. Each command button opens a query and a report.
I have set up a similar form in my database where I set the criteria in the query to equal the combo box value using OR LIKE ... IS NULL statements to allow null selections; however the form I am working on at the moment has more combos and I am getting an error message "The query is too complex"

I have come accross this piece of code on EE and was wondering if I could use it. The code as is filters a subform.

Could I set the "strSWC" in the form code, then set the "strSQL" in the on click event of each command button?

How would I modify the code to suite?

Any other suggestions would be much appreciated.

Please note these are not my form or control names this is an example posted on EE.

Sub Mutifilter()
Dim strSQL as String
Dim strSWC as string   'SQL where clause

If not isnull(Me.cboPN) Then
   strSWC =  strSWC & "PartnerName='" & Me.cboPM & "' AND "
End if

If Not isnull(Me.cboM) Then
   strSWC = strSWC & "Material='" & Me.cboM & "' AND "
End IF

' similar for spec no, approved by
...

'Strip off the final AND Clause
If Not strSWC = vbNulLString Then
  strSWC = Left(strSWC,Len(strSWC) - 5)
End IF

'Compose the SQL for the subforms's recordsource
strSQL = "SELECT * FROM <your subforms recordsource>"
If Not strSWC = vbNullstring Then
  strSQL = strSQL & " WHERE " & strSWC
End IF

'Now update the recordsource for subform
'NB where frmSubForm is the name oof the subform object on the main form
Me.frmSubFrom.Form.Recordsource = strSQL

End Sub
0
Comment
Question by:dommac82
  • 3
4 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22824916
dommac82,

Systems like this can be tricky and complex.

Most developers would move up to actually building the SqL in the code.

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22824940
0
 

Accepted Solution

by:
dommac82 earned 0 total points
ID: 22826375
Boag2000,

Thanks for the link. I think I found something in a post related to the link.
In one of the combos there are 1500 possibile selections; hence the cascading combos.

So I have simply created a string Where clause like in the example I gave, and added it to the Docmd.OpenReport statement. It works, but I am not confident. Do you see any potential problems doing it this way?

   Dim strWhere As String
    Dim rptLbyS As String
   

strWhere = "[Removed]=False AND [Fruit ID]='Blue'"
If Not IsNull(Me.cboType) Then
    strWhere = strWhere & "AND [Type ID]='" & Me.cboType & "'"
End If
If Not IsNull(Me.cboVariety) Then
    strWhere = strWhere & "AND [Variety]='" & Me.cboVariety & "'"
End If
If Not IsNull(Me.cboStage) Then
    strWhere = strWhere & "AND [Stage Name]='" & Me.cboStage & "'"
End If
If Not IsNull(Me.cboBlock) Then
    strWhere = strWhere & "AND [Block ID]='" & Me.cboBlock & "'"
End If
If Not IsNull(Me.cboSection) Then
    strWhere = strWhere & "AND [Section ID]='" & Me.cboSection & "'"
End If
If Not IsNull(Me.txtStartDate) Or Not IsNull(Me.txtEndDate) Then
    strWhere = strWhere & "AND [Planting Date] Between NZ('" & Me.txtStartDate & "',[Planting Date])"
    strWhere = strWhere & "AND NZ('" & Me.txtEndDate & "',[Planting Date])"
End If

'Set Report Name
rptLbyS = "rptBlueberryLocationBySection"

'Open Report
DoCmd.OpenReport rptLbyS, acPreview, , strWhere
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22827772
dommac82,

"Do you see any potential problems doing it this way"
Not really, ...hey as long as it gives you what you want...
;-)

JeffCoachman
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Open a Specific Record With a Specific Tab Page 5 31
Open CSV, modify and save as xls from Access 12 19
Two list boxes - best structure 3 32
access 7 25
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now