Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query data from multiple Dropdown selection on Form

Posted on 2014-01-29
4
Medium Priority
?
678 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
[X]
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
4 Comments
 
LVL 85
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 48

Accepted Solution

by:
Dale Fye earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

609 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