• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1393
  • Last Modified:

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

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
ssailer
Asked:
ssailer
  • 9
  • 8
1 Solution
 
mbizupCommented:
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
 
ssailerAuthor Commented:
Brilliant!  Then how would I translate it into a query, instead of outputting msgbox strCriteria?
0
 
mbizupCommented:
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
mbizupCommented:
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
 
ssailerAuthor Commented:
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
 
mbizupCommented:
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
 
ssailerAuthor Commented:
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
 
ssailerAuthor Commented:
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
 
mbizupCommented:
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
 
ssailerAuthor Commented:
It's opening the report, but the report is coming up blank - no records.  The underlying query is still showing all records.  Thanks.  
0
 
mbizupCommented:
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
 
mbizupCommented:
Also, is FileNumber a text or numeric field?
0
 
ssailerAuthor Commented:
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
 
mbizupCommented:
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
 
ssailerAuthor Commented:
EXCELLENT!!!
0
 
mbizupCommented:
Glad we got it ;-)
0
 
ssailerAuthor Commented:
Couldn't have done it without all of your help :D
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now