Listbox Requery (1 Viewer)

kwm

New member
Local time
Today, 09:31
Joined
Apr 22, 2019
Messages
3
I have a combo box with a list of values (categories) and a listbox(multivalue selection) which has the list values populated from a query based on the selected combo box value.

The listbox is set to update the values when the user selects a new value from the combo box. However I am having some issues in the following scenario:
  1. User already has selected some items in the listbox
  2. When changing the category, the selected item still stays checked even though it is supposed to be de-selected.

The code is as follows:

Code:
Dim varItem as Variant

For Each varItem in lstProducts.ItemsSelected

lstProducts.Selected(varItem) = False

Next varItem

lstProducts.Requery

I've tried to perform both the de-selection and listbox requery separately and they work fine, but when done together it seems to bug out. I've also tried to use the command - DoCmd.Requery (lstProducts) with the same result.

Hopefully someone has a solution to this problem. Thanks in advance :)
 

mike60smart

Registered User.
Local time
Today, 17:31
Joined
Aug 6, 2017
Messages
1,913
Hi

You say :-
"I have a combo box with a list of values (categories) and a listbox (multivalue selection) which has the list values populated from a query based on the selected combo box value."

I would not recommend using the Multivalue feature.

What you are describing is Cascading Combobox's.

Instead of having a Multivalue Selection it is recommended that you store each selection as a separate record rather than a value in a Multivalue field.
 

isladogs

MVP / VIP
Local time
Today, 17:31
Joined
Jan 14, 2017
Messages
18,207
I think this is just incorrectly described.
Reading the code, I believe the OP is referring to a multiselect listbox which is a perfectly good approach.

If so, can you clarify which event you used for the code supplied

However, if that's not the case, I would agree that multivalue fields (MVFs) are a very BAD idea. See http://www.mendipdatasystems.co.uk/multivalued-fields/4594468763
 

kwm

New member
Local time
Today, 09:31
Joined
Apr 22, 2019
Messages
3
Thanks for the replies so far. I've thought about finding other ways instead of using a multivalue field however the user would need to have to select multiple values as part of the selection (list length may vary depending on the combo box selection).

I've tried to use the "AfterUpdate()" and "On_Click()" events with the same result.

For additional context, the selected data will be appended to a table based on the selected items in the listbox.
I know this isn't the ideal method of doing it, but currently haven't been able to come up/seen a better way of doing this.
 

isladogs

MVP / VIP
Local time
Today, 17:31
Joined
Jan 14, 2017
Messages
18,207
You still seem to be describing a multiselect listbox and not a MVF.
These are very different things.
Please post a screenshot of what you are using. You'll need to zip it as you have less than ten posts.
 

kwm

New member
Local time
Today, 09:31
Joined
Apr 22, 2019
Messages
3
You still seem to be describing a multiselect listbox and not a MVF.
These are very different things.
Please post a screenshot of what you are using. You'll need to zip it as you have less than ten posts.

Yeah you're right, seems like I've been going down the wrong path (was previously using MVF to generate the listbox).

I've been trying to find a way to include a listbox with a checkbox but haven't been able to find a way around it yet.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:31
Joined
Oct 29, 2018
Messages
21,447
Yeah you're right, seems like I've been going down the wrong path (was previously using MVF to generate the listbox).

I've been trying to find a way to include a listbox with a checkbox but haven't been able to find a way around it yet.
Hi. It might be quicker to help you if you're able to post a sample copy of your db with test data. Just a thought...
 

isladogs

MVP / VIP
Local time
Today, 17:31
Joined
Jan 14, 2017
Messages
18,207
AFAIK you can't add checkboxes to a listbox. However there is no reason to do so as you have the multiselect feature available.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 17:31
Joined
Sep 12, 2006
Messages
15,634
I tried to do something similar from first principles recently.

What I had to do was this in the end.
1. save the values of the selected items in some structure (I use a string with a sensible separator to distinguish the selected items)
2. clear all the selected items
3. requery the list box
4. use the saved values to re-select the list box items,

I assume the selected items still relate to the re-queried list box items.

It wasn't so obvious, and it took a fair while.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:31
Joined
May 21, 2018
Messages
8,519
See if this code helps. This has all the steps for using a multi select listbox as a control.
1. Clear the old selections
2. Hilite the selections for that record
3. Add delete a selection from a table


Code:
Private Sub Form_Current()
 ' The On Current event. Clear the selections then hilite the selections."
  ClearSelections Me.lstProducts
  If Not Me.NewRecord Then
    ShowSelections Me.lstProducts, Me.OrderID
  End If
End Sub
Public Sub ShowSelections(lst As Access.ListBox, ForeignKey As Long)
  Dim RS As DAO.Recordset
  Dim strSql As String
  Dim varItm As Variant
  Dim SelectedID As Long
  Dim i As Integer
  'Need to get a recordset of the items to hilite in the listbox
  strSql = "Select ProductID from qryOrders_Products where OrderID = " & ForeignKey
  Set RS = CurrentDb.OpenRecordset(strSql)
  'loop the recordset
  Do While Not RS.EOF
    SelectedID = RS!ProductID
    'loop the items in the listbox to find a match
    For i = 0 To (lst.ListCount - 1)
      Debug.Print lst.Column(0, i) & " " & SelectedID
      If lst.Column(0, i) = SelectedID Then
        lst.Selected(i) = True
        Exit For
      End If
    Next i
    RS.MoveNext
  Loop
End Sub
Public Sub ClearSelections(lst As Access.ListBox)
  Dim i As Integer
  For i = 0 To (lst.ListCount - 1)
     lst.Selected(i) = False
   Next i
End Sub

Private Sub lstProducts_AfterUpdate()
  If Not IsNull(Me.OrderID) Then
    AddRemoveSelections Me.OrderID
    Me.SubFrmOrderProducts.Form.Requery
  Else
    MsgBox "Create the order first by giving it an order date."
  End If
End Sub
Public Sub AddRemoveSelections(OrderID As Long)
  Dim lst As Access.ListBox
  Dim RS As DAO.Recordset
  Dim FocusedItemIndex As Integer
  Dim strSql As String
  Dim ProductID As Long
  Set lst = Me.lstProducts
  FocusedItemIndex = lst.ListIndex
  ProductID = lst.Column(0, FocusedItemIndex)
  'The selected property is an array of all items and holds a true false if the item is selected
   If lst.Selected(FocusedItemIndex) Then
     'If the item is focused and selected then add it.Add to list
     strSql = "Insert INTO tblOrder_Products (OrderID, ProductID) VALUES (" & OrderID & ", " & ProductID & ")"
   Else
     'If the item is focused but not selected then delete it
     strSql = "DELETE * FROM tblOrder_Products where OrderID = " & OrderID & " AND ProductID = " & ProductID
   End If
   Debug.Print strSql
   CurrentDb.Execute strSql
End Sub
 

Users who are viewing this thread

Top Bottom