Add Multivalued List Box to Search Form (1 Viewer)

freidaf

Registered User.
Local time
Today, 05:35
Joined
Aug 13, 2012
Messages
44
I have created a search form based on code from Allen Browne (see attached) that works very well, however, I need to add a couple of list boxes to the form. I want the user to be able to select multiple values from the list. Can someone give me an example of adding 2 list boxes to the attached code sample? I can't figure out how to incorporate the list boxes into the code.

Thank you!
 

freidaf

Registered User.
Local time
Today, 05:35
Joined
Aug 13, 2012
Messages
44
I'm sorry, let me try again.
 

Attachments

  • Search Form.doc
    34.5 KB · Views: 56

jdraw

Super Moderator
Staff member
Local time
Today, 08:35
Joined
Jan 23, 2006
Messages
15,378
How about telling readers a little about your database; the tables and relationships you have; and what the new list boxes are for (in business terms); and show us a sample of your search that is working?

It is difficult to offer focused advice/suggestions when we have no business context or samples.

Good luck with your project.
 

freidaf

Registered User.
Local time
Today, 05:35
Joined
Aug 13, 2012
Messages
44
Yes, Data entry operators enter employee’s production volumes and time spent on machines from production logs that the employees fill out every day. Some fields on the form are Employee Name, Job Number, Job Type, Machine Name, Total Time, and Total Volume Produced. I currently have combo boxes for Job Type, and Machine Name but I would like them to be list boxes so that the data entry operators could search for records on multiple jobs and/or machines. When the filter button is pressed, the table records are filtered. When the remove filter button is pressed, all table records are displayed again. The form works very well as is but I really need to replace the combo boxes with list boxes. I have attached a print screen of the form on Allen Browne's website that I used to create my form. I am unable to post a copy of my form due to company policy.
 

Attachments

  • Doc3.doc
    38.5 KB · Views: 53

freidaf

Registered User.
Local time
Today, 05:35
Joined
Aug 13, 2012
Messages
44
I think I am getting closer with this list box but received the following error:
Run time error '3075'
Invalid use of '.','!', or '()'. in query expression '[lstMachID] IN ("E148","E161","E995")([WorkTypeID] = "P2R") AND ([ActivityID]="P") AND ({ProdDate] >=#06/15/2017#) AND ([ProdDate] < #06/16/2017#'.

lstMachID is my listbox with E148, E161, and E995 being the machine names I selected from the listbox. It looks like I am missing the "AND" between the machine selections and the WorkTypeID field.

Could you tell me what is wrong with my listbox code below?


Private Sub cmdFilter_Click()
'Purpose:Build up the criteria string from the non-blank search boxes, and apply to the form's Filter.
'Notes:1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
we remove the trailing " AND " at the end.
'2. The date range works like this: _
Both dates= only dates between (both inclusive. _
Start date only = all dates from this one onwards; _
End date only= all dates up to (and including this one).
Dim strWhere As String'The criteria string.
Dim lngLen As Long'Length of the criteria string to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#"'The format expected for dates in a JET query string.
Dim varItem As Variant
Dim strCriteria As String
Dim strDelim As String'Delimiter for this field type
Dim strDescrip As String
Dim ctl As Control
strDelim = """"

'***********************************************************************
'Look at each search box, and build up the criteria string from the non-blank ones.
'***********************************************************************
'List box example by VF
'Loop through the ItemsSelected in the list box.
With Me.lstMachID
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column
strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
'Build up the description from the text in the visible column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "

End If
Next
End With

'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[lstMachID] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "MachineType: " & Left$(strDescrip, lngLen)
End If
End If
 

jdraw

Super Moderator
Staff member
Local time
Today, 08:35
Joined
Jan 23, 2006
Messages
15,378
Can you step through your code and debug.print your evolving query code to the immediate window? When you finish building the IN statement, you have to append an AND.

Stepping thru the code will help pin point the place where the AND must be added.
Good luck.
 
Last edited:

freidaf

Registered User.
Local time
Today, 05:35
Joined
Aug 13, 2012
Messages
44
I found it! Here is where I added the "AND":

strWhere = "[lstMachID IN (" & Left$(strWhere, lngLen)& ") AND "

Thank you for your help.
 

jdraw

Super Moderator
Staff member
Local time
Today, 08:35
Joined
Jan 23, 2006
Messages
15,378
Good work.
Glad you have it resolved.
 

Users who are viewing this thread

Top Bottom