Solved

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

Posted on 2011-09-12
17
1,161 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
  • 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

932 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

10 Experts available now in Live!

Get 1:1 Help Now