?
Solved

access 2010 - create a query based on multiple values from a listbox

Posted on 2011-09-12
17
Medium Priority
?
1,343 Views
Last Modified: 2012-05-12
I have a listbox named List2.  My form is named frmMulti.  I want to select multiple values from the listbox and filter a query based on my selections.  fileNumber is the unique field within the recordsource of the listbox.  I'm having trouble getting the query to filter properly based on the selections of the listbox.  Thanks!
0
Comment
Question by:ssailer
[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
  • 9
  • 8
17 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36526663
This shows how you would build criteria for FileNumber based on the selections from a multi-select listbox:

Private Sub YourCommandButton_Click()
Dim strCriteria As String
Dim varItem As Variant

    If Me.List2.ItemsSelected.Count = 0 Then
        MsgBox "Nothing was selected"
        Exit Sub
    End If
    
    For Each varItem In Me.List2.ItemsSelected
        strCriteria = strCriteria & Me.List2.ItemData(varItem) & ","
    Next
    If Right(strCriteria, 1) = "," Then strCriteria = Left(strCriteria, Len(strCriteria) - 1)
    strCriteria = "fileNumber  IN (" & strCriteria & ")"    
    msgbox strCriteria
    
End Sub

Open in new window

0
 

Author Comment

by:ssailer
ID: 36527083
Brilliant!  Then how would I translate it into a query, instead of outputting msgbox strCriteria?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36527820
As a rule you don't want users accessing data directly through tables or queries.  The typical use for this type of criteria is to filter down records in a table or form to display what the user needs.


So you would create a form with a record source consisting of the whole table and filter the records when opening the form like this :

Docmd.opeform "formname",,,strcriteria
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 61

Expert Comment

by:mbizup
ID: 36528261
Alternatively, you could build the full SQL string for a query based on the criteria like this:

strSQL = "SELECT * FROM YourTable WHERE " & strCriteria

strSQL could then be applied in a variety of ways:

Me.MyListBox.Rowsource = strSQL
Me.MyForm.Recordsource = strSQL
etc...


It all depends on what you need to do.

Can you describe how you want to use the resulting query?
0
 

Author Comment

by:ssailer
ID: 36529039
Ideally, what I want to do is allow the user to select a bunch of file numbers from the list box, have the query filtered based on the selections (the query would be the underlying record source for a summary report as well), and then create another process for looping through the email addresses and sending a PDF report to each recipient.  I have already created a process for emailing a report to ONE person from a combo box, but I would need to create a different process for emailing a group of people as well.  Thanks.  I haven't worked with Access for about ten years, so I feel like I'm starting over with this one...
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36529369
Okay - thats very similar to the form scenario I described in my last comment.

To get this up and running, create your report using a recordsource with no criteria (you should see all records in it).

Then open the report after setting up the criteria string like this

Private Sub YourCommandButton_Click()
Dim strCriteria As String
Dim varItem As Variant

    If Me.List2.ItemsSelected.Count = 0 Then
        MsgBox "Nothing was selected"
        Exit Sub
    End If
    
    For Each varItem In Me.List2.ItemsSelected
        strCriteria = strCriteria & Me.List2.ItemData(varItem) & ","
    Next
    If Right(strCriteria, 1) = "," Then strCriteria = Left(strCriteria, Len(strCriteria) - 1)
    strCriteria = "fileNumber  IN (" & strCriteria & ")"    
    Docmd.openreport YourReportName, acViewPreview,, strCriteria    '<----- Add this
End Sub 

Open in new window


Using a WHERE condition in the OpenReport statement effectively and dynamically adds a WHERE clause (criteria) to the query that you are using as the recordsource of your report.

For the emailing routine, go ahead and post a seperate question.   Many of the Access Experts have sample code to do what you have described.
0
 

Author Comment

by:ssailer
ID: 36529460
I created a report called rptSUMMARY, with the underlying query record source containing NO criteria.  It's causing an error at this line in the code:  
DoCmd.OpenReport rptSUMMARY, acViewPreview, , strCriteria

The error is run-time error 2497.  

The action or method requires a Report Name argument.

Thank you!
0
 

Author Comment

by:ssailer
ID: 36529482
This is my query:

SELECT tblFiles.fileNumber, tblFiles.client, tblFiles.Contact, tblFiles.email, tblFiles.advparty, tblFiles.resident, tblFiles.fileopened, tblFiles.facility, tblFiles.amtclaimed, tblFiles.amtcollect, tblFiles.telephone, tblFiles.c_claimamt, tblFiles.update, tblFiles.inactive, tblFiles.who, tblFiles.dcfilenum, tblFiles.resppartner, tblHistory.notes
FROM tblFiles LEFT JOIN tblHistory ON tblFiles.fileNumber = tblHistory.fileNumber;

I need to join two tables together to get all of the related data to show up on the report.  
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36529512
Here you go -

The report name needs to be in quotes if it is a literal (non-variable) value:

DoCmd.OpenReport "rptSUMMARY", acViewPreview, , strCriteria

Open in new window

0
 

Author Comment

by:ssailer
ID: 36530458
It's opening the report, but the report is coming up blank - no records.  The underlying query is still showing all records.  Thanks.  
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36530503
That generally means that there are no records matching the criteria specified (ie: your listbox selections).

Verify that the fileNumber selections you have made are indeed present in your query results.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36530541
Also, is FileNumber a text or numeric field?
0
 

Author Comment

by:ssailer
ID: 36530789
FileNumber is a text field.  This is the way it was originally set up when I inherited it.  Yes, there were records present when I manually ran the query based on a list box selection.  
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 36530833
Okay - if it is a text field, then each criteria selected needs to be enclosed in quotes to delimit it as text.

Revise the function I gave you like this:

Private Sub YourCommandButton_Click()
Dim strCriteria As String
Dim varItem As Variant

    If Me.List2.ItemsSelected.Count = 0 Then
        MsgBox "Nothing was selected"
        Exit Sub
    End If
    
    For Each varItem In Me.List2.ItemsSelected
        strCriteria = strCriteria & "'" & Me.List2.ItemData(varItem) & "',"  '<--- I've added single quotes here to delimit Text filenumbers
    Next
    If Right(strCriteria, 1) = "," Then strCriteria = Left(strCriteria, Len(strCriteria) - 1)
    strCriteria = "fileNumber  IN (" & strCriteria & ")"    
    Docmd.openreport YourReportName, acViewPreview,, strCriteria    '<----- Add this
End Sub 

Open in new window

0
 

Author Closing Comment

by:ssailer
ID: 36533050
EXCELLENT!!!
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36533097
Glad we got it ;-)
0
 

Author Comment

by:ssailer
ID: 36533109
Couldn't have done it without all of your help :D
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 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