Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Query data from multiple Dropdown selection on Form

Posted on 2014-01-29
4
669 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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. …
Outlook Free & Paid Tools
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

856 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