ListBox - selecting rows.

  • Thread starter Thread starter Jim McDonald
  • Start date Start date
J

Jim McDonald

Hi all - any ideas on this one?
I have a listbox with ten rows, from which I select say 4 using extended
multiselect.
How do I later reload the selection back into the listbox so that it shows
the original selection?
Thanks
Jimbo Macca
 
Jimbo,
I do this using the following method - kind of messy, but it works. General
idea is to save the selected item numbers in another field, then read them
back in when you re-open the form

Control r06c3 is a multi-select list box, one of several on this screen
Private Sub r06c3_AfterUpdate()
Dim e, Itm
Me!r06Label = Null
For Each Itm In Me!r06c3.ItemsSelected
Me!r06Label = Me!r06Label & Format(Me!r06c3.Column(0, Itm), "00")
Next
End Sub

then in the Open event:
Private Sub Form_Open(Cancel As Integer)
Dim e, x, y
'put multi-select choices back into list boxes (stored in "r0yLabel" fields)
For y = 2 To 6
For x = 0 To Me("r0" & y & "c3").ListCount - 1
If InStr(1, Me("r0" & y & "Label"), Format$(Me("r0" & y &
"c3").Column(0, x))) Then
Me("r0" & y & "c3").Selected(x) = True
End If
Next
Next

End Sub

HTH,
John
 
Jimbo,

Piece of Cake .. or pie if youlike that better!

You will have to open up the recordset then test for each
case in a Do... Loop or in a For .. Next statement. Set
the listbox property of Selected(count_number) = True for
those cases in which your recordset equals the bound
column of the list box.


Here is an example:

With earst
.Open "SELECT
tbl_Contact_Address_Join.Contact_Address_Type_ID,
tbl_Contact_Address_Join.Contact_Address_ID " & _
"FROM tbl_Contact_Address_Join WHERE
(((tbl_Contact_Address_Join.Contact_Address_ID)=" &
Me.txtAddressID & "));", CurrentProject.Connection,
adOpenKeyset, adLockOptimistic
addtypecount = .RecordCount
.MoveFirst

Me.lst_AddressType.Requery
'This makes them all unselected just in case
Dim k
For k = 0 To Me.lst_AddressType.ListCount - 1
Me.lst_AddressType.Selected(k) = False
Next
'This will select the item in the list box if there is a
matching record
Do Until .EOF
For k = 0 To Me.lst_AddressType.ListCount - 1
If CLng(Me.lst_AddressType.Column(0, k)) = !
Contact_Address_Type_ID Then Me.lst_AddressType.Selected
(k) = True
Next
.MoveNext
Loop
.Close
End With
 
Hey! Thanks guys - a working answer in rapid time. Brilliant!! thanks a
million!

Jimbo
 
Back
Top