Save and retrieve listbox selections

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with several listboxes each allowing multi-select.
The form allows all of these selections to be saved under a name (to a
special table) and also retrieved from the table.
This seems to work but the problem is that when they retrieve the settings
and I update the various '.selected' values, the listboxes dont show the
selections. I tried doing repaint, refresh and requery but nothing works.
What do I have to do to get them to show up? Here is my code to reload one
listbox:
Public Sub IncCat(strName As String)
Dim i As Integer
With Me!lstIncCats
For i = 0 To .ListCount - 1
If .ItemData(i) = strName Then
.Selected(i) = True
End If
Next
.Requery
End With
End Sub
 
In
mscertified said:
I have a form with several listboxes each allowing multi-select.
The form allows all of these selections to be saved under a name (to a
special table) and also retrieved from the table.
This seems to work but the problem is that when they retrieve the
settings and I update the various '.selected' values, the listboxes
dont show the selections. I tried doing repaint, refresh and requery
but nothing works. What do I have to do to get them to show up? Here
is my code to reload one listbox:
Public Sub IncCat(strName As String)
Dim i As Integer
With Me!lstIncCats
For i = 0 To .ListCount - 1
If .ItemData(i) = strName Then
.Selected(i) = True
End If
Next
.Requery
End With
End Sub

I think your problem is probably with this line:

Requerying a list box normally clears all selections in it. I would
expect it to work if you take that line out -- I use code very like it,
successfully. You can also exit early once you've found the row you're
looking for:

'----- start of revised code -----
Public Sub IncCat(strName As String)
Dim i As Integer
With Me!lstIncCats
For i = 0 To .ListCount - 1
If .ItemData(i) = strName Then
.Selected(i) = True
Exit For
End If
Next
End With
End Sub
'----- end of revised code -----
 
Back
Top