Solved

Query data from multiple Dropdown selection on Form

Posted on 2014-01-29
4
668 Views
Last Modified: 2014-02-06
Hello,

I have a Query which creates a Report. The query data is provided by dropdowns from a Form.
Different groups of people need different views (I currently have 3 queries which produce 3 reports which have the exact format). So far I have duplicated the query and the reports depending on the selection made from the form. Some selection may be left empty (no slection).
I’m sure I can reduce all the duplications however I don’t know how.

Basically I would like selection to be made in the form to produce one report and not 3 which is the case now.

Please find attached a sample file of the queries I have used.

Your help you be greatly appreciated.
QuerySample.txt
0
Comment
Question by:dnt2009
4 Comments
 
LVL 84
ID: 39817346
I usually "build up" my query in the form, and then use that as the RecordSource of the report. For example, in your form with the dropdowns, you'd do this before opening your report:

Dim sWhere as string

If Me.cboChannel<>"All" Then
  sWhere = " tblItems.Channel='" & Me.cboChannel & "'"
End If

If Me.cboItemStatus <> "ALL" Then
  If Len(sWhere) > 0 Then
    sWhere = sWhere & " AND "
  End If
  sWhere = sWhere & " tblItems.ItemStatus2='" & Me.cboItemStatus & "'"
End If

Open in new window

And so on for your other combos. Remember that String datafields are enclosed in single quotes ( ' ) and Dates are enclosed in hashmarks ( # )

After you've builtup your sWhere, do this:

If Len(sWhere) > 0 Then
  sWhere = "SELECT tblItems.ItemID, tblItems.Channel, tblItems.ItemName, tblItems.ItemDescription, tblItems.DateRaised, tblItems.ItemRaisedBy, tblItems.ItemCategory," _
		& "tblItems.ItemSmashingSuggestion, tblItems.DateAssigned, tblItems.ItemOwner, tblItems.ItemDelegate, tblItems.ItemChampion, tblItems.ItemsParticipants," _
		& "tblItems.DueDate, tblItems.DateClosed, tblItems.ItemStatus, tblItems.ItemSubStatus, tblItems.ItemRAG, tblItems.HowItemSmashed, tblItems.ItemStatus2," _
		& "tblItemsUpdates.DateofUpdate, tblItemsUpdates.UpdatedBy, tblItemsUpdates.ItemUpdate" _
		& " FROM ((SELECT tblItemsUpdates.ItemID, Max(tblItemsUpdates.DateofUpdate) AS MaxOfDateofUpdate FROM tblItemsUpdates GROUP BY tblItemsUpdates.ItemID)  AS" _
		& "  qry_LastUpdate0 LEFT JOIN tblItemsUpdates ON (qry_LastUpdate0.ItemID = tblItemsUpdates.ItemID) AND (qry_LastUpdate0.MaxOfDateofUpdate =" _
		& " tblItemsUpdates.DateofUpdate)) RIGHT JOIN tblItems ON qry_LastUpdate0.ItemID = tblItems.ItemID WHERE " & sWhere
End If

Open in new window


You should now have a fully formed SQL statement. Create your report, and use a stored query as the Recordsource (let's name it qryUpdates), then continue the code like this:

Dim qdf As DAO.QueryDef
Set qdf = Currentdb.QueryDefs("qryExport")
qdf.sql = sWhere

'Now open the report:
DoCmd.OpenReport "YourReport", acViewPreview
'/ cleanup
Set qdf = nothing

Open in new window

0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39817349
I do this via code, which gives you more flexibility and significantly reduces the complexity of the resulting filter string.  On your form, I assume you have a command button to open the report.  In the click event of that button place the following code:

Private Sub cmd_Report_Click

    Dim varFilter as Variant

    varFilter = NULL

    if NZ(me.cboChannel, "ALL") <> "ALL" THEN
        varFilter = "([tblItems].[Channel] = " & me.cboChannel & ")"
    endif

    if NZ(me.cboItemStatus, "ALL") <> "ALL" Then
        varFilter = (varFilter + " AND ") _
                       & "([tblItems].[ItemStatus2] = " & me.cboItemStatus & ")"
    end if

    if NZ(me.cboItemStatusChamps, "ALL") <> "ALL" Then
        varFilter = (varFilter + " AND ") _
                       & "([tblItems].[ItemStatus2] = " & me.cboItemStatusChamps & ")"
    end if

    if NZ(me.cboItemChampion, "ALL") <> "ALL" Then
        varFilter = (varFilter + " AND ") _
                       & "([tblItems].[ItemChampion] = " & me.cboItemChampion & ")"
    end if

    if NZ(me.cboItemOwner, "ALL") <> "ALL" Then
        varFilter = (varFilter + " AND ") _
                       & "([tblItems].[ItemOwner] = " & me.cboItemOwner & ")"
    end if

    if NZ(me.cboItemStatusOwners, "ALL") <> "ALL" Then
        varFilter = (varFilter + " AND ") _
                       & "([tblItems].[ItemStatus2] = " & me.cboItemStatusOwners & ")"
    end if

    Debug.Print varFilter
    Docmd.OpenReport "YourReportName", acViewPreview, , NZ(VarFilter, "")

End Sub

Open in new window

This code will build your criteria at run time, rather than having to store separate queries for the same report.  It assumes that all of the combo boxes have numeric fields as their bound columns.  It that is not correct, you will need to add a single quote immediately before the double quote following the = and then another immediately before the closing parenthesis, like:

           & "([tblItems].[ItemStatus2] = '" & me.cboItemStatusOwners & "')"

Each of the If statements tests to determine whether a particular combo box contains a value other then "ALL" if it is "ALL" it ignores that criteria as it is unnecessary to add it to the filter.

Then inside the IF statement, the line:

        varFilter = (varFilter + " AND ") _

simply adds " AND " to the filter if varFilter has a value other than NULL.  You may or may not be aware of this, but when you use the + to concatenate a value to a NULL, it will return NULL, but if you use an &, it will return a value:

NULL + " AND " = NULL
NULL & " AND " = " AND "

The line following that simply creates the new criteria for that particular combo box.

Hope this helps.
0
 
LVL 4

Expert Comment

by:Jack Leach
ID: 39817359
In furtherance to Scott and Dale's responses, I have a blog post which describes how to dynamically build a filter based on text input and multi-select list boxes, which you could use to adapt to your situation:

http://www.dymeng.com/blog/DynamicFilters

Cheers,
-jack
0
 

Author Comment

by:dnt2009
ID: 39838818
Hi all - thank you for all your ipnuts. I used fryed's code it was the method worked great for my requirements.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

770 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