Solved

Query data from multiple Dropdown selection on Form

Posted on 2014-01-29
4
664 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

743 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

20 Experts available now in Live!

Get 1:1 Help Now