Listbox selection(s) to open/display records

ChrisCione

Board Regular
Joined
Aug 27, 2008
Messages
92
Office Version
  1. 365
Platform
  1. Windows
Hello,

I found the code below from a very old thread and am attempting to modify. It is hanging on the italicized line, prompting me for a Parameter Value for cboSpecialist. cboSpecialist is the name of the field on the form that I am using as the filter. I.e, match the lstSpecialist selection(s) with cboSpecialist field and display only those records that match.

strFilter = strFilter & "cboSpecialist = """ & Me![lstSpecialist].ItemData(varNumber) & """"

Code:
Private Sub Command79_Click()
On Error GoTo Err_Command79_Click
Dim varNumber As Variant
Dim strFilter As String
If Me.lstSpecialist.ListCount = 0 Then
 MsgBox "Please select a Specialist.", vbOKOnly + vbExclamation, "Select a Specialist"
Else
 For Each varNumber In Me![lstSpecialist].ItemsSelected
  If strFilter <> "" Then
   strFilter = strFilter & " or "
  End If
    strFilter = strFilter & "cboSpecialist = """ & Me![lstSpecialist].ItemData(varNumber) & """"
 Next varNumber
 DoCmd.OpenForm "frmRecruitment", acNormal, , strFilter
End If
Exit_Command79_Click:
 Exit Sub
Err_Command79_Click:
 MsgBox Err.Description
 Resume Exit_Command79_Click
 
End Sub

Many thanks for any help.
 
Bob,

I made one minor modification, from this

strFilter = "[cboSpecialist] In(" & strFilter & ")"

to

strFilter = "[Specialist] In(" & strFilter & ")"

The original statement was pointing to a combo box on the form; the modified points to the field in the table.
I had wondered about that but I have seen people name their fields some strange stuff so I didn't ask about it. :biggrin:
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
One minor issue:

When no selection is made in the listbox, it throws up the syntax error:

Syntax error (missing operator) in query expression 'cboSpecialist IN

instead of Please select a Specialist. from

If Me.lstSpecialist.ListCount = 0 Then
MsgBox "Please select a Specialist.", vbOKOnly + vbExclamation, "Select a Specialist"
Else


Any idea how to fix that?
 
Last edited:
Upvote 0
One minor issue:

When no selection is made in the listbox, it throws up the syntax error:

Syntax error (missing operator) in query expression 'cboSpecialist IN

instead of Please select a Specialist. from

If Me.lstSpecialist.ListCount = 0 Then
MsgBox "Please select a Specialist.", vbOKOnly + vbExclamation, "Select a Specialist"
Else


Any idea how to fix that?

How about
Code:
If Me.lstSpecialist.ItemsSelected.Count = 0 Then
 
Upvote 0
Perfect.

Bob, you're hired. :biggrin:

Those are 3 words I've been longing to hear from my supervisor at my 6 month contract-to-hire job for almost 2½ years.

headinhands.jpg
 
Upvote 0

Forum statistics

Threads
1,225,149
Messages
6,183,178
Members
453,151
Latest member
Lizamaison

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top