Solved

Multifilter for Report

Posted on 2008-10-27
4
271 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

707 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

9 Experts available now in Live!

Get 1:1 Help Now