?
Solved

Query data from multiple Dropdown selection on Form

Posted on 2014-01-29
4
Medium Priority
?
676 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

801 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