Access 2016 (1 Viewer)

slj

Registered User.
Local time
Today, 02:51
Joined
May 11, 2019
Messages
16
The boss has asked me to create a form based on a query that lets him
1. Select from a list of Groups in a listbox ,
2. in listbox 2 (multi-select) shows all the Companies & Contacts in that Group,
3. in listbox 3 show which contacts he has selected.

He then wants to print a report of only the selections shown in listbox 3.

Can this been done and how? Any ideas? I am at a loss to see how to do this.:banghead:
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:51
Joined
Oct 29, 2018
Messages
21,498
Hi. Which part are you having difficulties with? Take the tasks one job at a time and let us know which specific area is not working.
 

June7

AWF VIP
Local time
Today, 00:51
Joined
Mar 9, 2014
Messages
5,488
What you describe is called cascading combobox or listbox. A very common topic. Google or search forum and you will find many discussions and examples.

For example of a search form, review http://allenbrowne.com/ser-62.html

Using a multi-select listbox as criteria to open a filtered report requires VBA code that loops through listbox selected items. The link will lead to example of that as well. The 3rd listbox is not really needed.
 

slj

Registered User.
Local time
Today, 02:51
Joined
May 11, 2019
Messages
16
Thanks June7, That is exactly what I needed to do. ;)
 

Micron

AWF VIP
Local time
Today, 04:51
Joined
Oct 20, 2018
Messages
3,478
3rd maybe not needed, but if boss wants it...
If the 3rd is a requirement, then you basically would put 2 (or 4) buttons between 2nd and 3rd. Right arrow to move selected items to 3rd list (assuming they go from left to right), left to remove them from 3rd list. Other two buttons, if used, would be to move all items either to or from 3rd. If you have the option, I suggest avoiding the 3rd list as it can get complicated for novices. In that case, either type of migration usually involves removing selected items from one list and placing it in another and rebuilding lists, which is not as easy as cascading and simply multi-selecting from list2.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Sep 12, 2006
Messages
15,662
Actually using the selected items in a list box at run time is one thing. Storing a single selected item in a list box (or combo box) is easy. But storing a list of multiple items selected in a list box for future use is not so easy.
 

slj

Registered User.
Local time
Today, 02:51
Joined
May 11, 2019
Messages
16
I am so close that I can taste victory over this monster. :banghead:

All my list boxes behave as expected, but when I run the report it is displaying the entire group not just the companies that were selected.

ListBoxes:
lstGroup
lstLeft
lstRight - is really just a security blanket for the boss, so that he can see which selections he made.

My code on the Preview Report button is:
Private Sub cmdGroupPreview_Click()
On Error GoTo Err_cmdGroupPreview_Click
Dim strWhere As String
Dim ctl As Control
Dim varItem As Variant

'make sure a selection has been made
If Me.lstLeft.ItemsSelected.Count = 0 Then
MsgBox "Must select at least 1 Company"
Exit Sub
End If

'add selected values to string
Set ctl = Me.lstLeft
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ","
'Use this line if your value is text
'strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
Next varItem
'trim trailing comma
strWhere = Left(strWhere, Len(strWhere) - 1)

'open the report, restricted to the selected items
DoCmd.OpenReport "rtGroupCo", acPreview, , "GroupCode IN(" & strWhere & ")"

Exit_cmdGroupPreview_Click:
Exit Sub

Err_cmdGroupPreview_Click:
MsgBox Err.Description
Resume Exit_cmdGroupPreview_Click

End Sub

What have I done wrong here, because I know it's is totally my mistake.
Thanks so much.
 

slj

Registered User.
Local time
Today, 02:51
Joined
May 11, 2019
Messages
16
I'm using pbauldy's code to filter a report by a multi-select listbox.

I am so close that I can almost taste victory, but I am stuck. All my list boxes behave as expected (see Pic). But when I run the report, it displays all the members of the group, not just the members selected.

List Boxes
lstGroup
lstLeft
lstRight - Just a security blanket for the boss so that he can see what he has selected before running the report.

On Click report preview button.
Code:
Private Sub cmdGroupPreview_Click()
    On Error GoTo Err_cmdGroupPreview_Click

  Dim strWhereGroup As String
  Dim ctl           As Control
  Dim varItem       As Variant

  'make sure a selection has been made
  If Me.lstLeft.ItemsSelected.Count = 0 Then
    MsgBox "Must select at least 1 Company"
    Exit Sub
  End If

  'add selected values to string
  Set ctl = Me.lstLeft
  For Each varItem In ctl.ItemsSelected
    strWhereGroup = strWhereGroup & ctl.ItemData(varItem) & ","
  Next varItem
  'trim trailing comma
  strWhereGroup = Left(strWhereGroup, Len(strWhereGroup) - 1)
  
  'open the report, restricted to the selected items
  DoCmd.OpenReport "rtGroupCo", acPreview, , "GroupCode IN(" & strWhereGroup & ")"

Exit_cmdGroupPreview_Click:
  Exit Sub

Err_cmdGroupPreview_Click:
  MsgBox Err.Description
  Resume Exit_cmdGroupPreview_Click

End Sub

I just can't figure out where I have gone wrong here. I tried to add a second Where stmt, but I don't think I entered it correctly because the report came back empty.

Any suggestions would be greatly appreciated. I have been trying to solve this on my own since May, but I have had no luck. Nothing I have tried has worked. I have read 3 books on VBA and the 2016 Access Bible but still am lost on how to fix this.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:51
Joined
Oct 29, 2018
Messages
21,498
Hi. Just before the DoCmd.OpenReport line, try adding this:
Code:
Debug.Print strWhereGroup
and let us know what you get in the Immediate Window. If you don't know what I mean, try using
Code:
MsgBox strWhereGroup
and post a screenshot here.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:51
Joined
Aug 30, 2003
Messages
36,128
Oh, and post 8 was moderated, I just approved it.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:51
Joined
Aug 30, 2003
Messages
36,128
Did you select the same item twice? Or is the bound column of the listbox not the appropriate field for this?
 

slj

Registered User.
Local time
Today, 02:51
Joined
May 11, 2019
Messages
16
Good question. I have the report based off of a query
Capture.JPG

query.jpg

lstGroup
RowSource:
SELECT DISTINCT [qryGroupCoContacts].[GroupCode], [qryGroupCoContacts].[GroupDesc] FROM qryGroupCoContacts ORDER BY [GroupDesc];

lstLeft:
RowSource:
SELECT DISTINCT qryGroupCoContacts.GroupCode, qryGroupCoContacts.Company, qryGroupCoContacts.Contact, qryGroupCoContacts.CompanyRef, qryGroupCoContacts.ContactRef FROM qryGroupCoContacts WHERE (((qryGroupCoContacts.GroupCode)=[Forms]![frmGroup]![lstGroup]));
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:51
Joined
Aug 30, 2003
Messages
36,128
I don't see how 2 could be returned twice if that's the group listbox to the left that the code is looking at. The report does seem to be filtered to the selected group though. Can you attach the db here?
 

slj

Registered User.
Local time
Today, 02:51
Joined
May 11, 2019
Messages
16
I have attached a copy of the db
 

Attachments

  • IntegritySales Sandbox.zip
    864.7 KB · Views: 57

Users who are viewing this thread

Top Bottom