ListBox Select

  • Thread starter Thread starter DS
  • Start date Start date
D

DS

I'm trying to selected a record in a listbox based on a record from
another form. When I click on the command button I want to highlight
the row in the listbox that matches the textbox.
Any help Appreciated. Thanks
DS

Forms!MenuMaker!ListSections.Column(1) = (Me.TxtSectionID)
 
Not quite as simple as it sounds. You must loop through the list box's list
of data to find the row whose ItemData value matches the value you want to
highlight, then set the Selected property of that row to True.

Note that setting the Selected property does not assign the value of that
row to the list box itself. You must do that directly in the code.

Also, you'll want to run this code from the form that contains the listbox.

Dim lngList As Long
With Me.ListSections
For lngList = 0 To .ListCount - 1
If .ItemData(lngList) = Forms!FormName!TxtSectionID.Value Then
.Selected(lngList) = True
.Value = .ItemData(lngList)
Exit For
End If
Next lngList
End With
 
Ken said:
Not quite as simple as it sounds. You must loop through the list box's list
of data to find the row whose ItemData value matches the value you want to
highlight, then set the Selected property of that row to True.

Note that setting the Selected property does not assign the value of that
row to the list box itself. You must do that directly in the code.

Also, you'll want to run this code from the form that contains the listbox.

Dim lngList As Long
With Me.ListSections
For lngList = 0 To .ListCount - 1
If .ItemData(lngList) = Forms!FormName!TxtSectionID.Value Then
.Selected(lngList) = True
.Value = .ItemData(lngList)
Exit For
End If
Next lngList
End With
Thanks, This has been driving me crazy. I'll give your code a try. In
the meantime I came up with this, which seems to work. Maybe there is a
downside?

Forms!MenuMaker!ListSections.SetFocus
Forms!MenuMaker!ListSections.Value = Me.TxtSectionID

Thanks
DS
 
DS said:
Thanks, This has been driving me crazy. I'll give your code a try. In
the meantime I came up with this, which seems to work. Maybe there is a
downside?

Forms!MenuMaker!ListSections.SetFocus
Forms!MenuMaker!ListSections.Value = Me.TxtSectionID

Thanks
DS
Your Code works Great!
DS
 
Your two lines of code will set the listbox to the desired value, but the
code does not actually select the row in the listbox.
 
Ken said:
Your two lines of code will set the listbox to the desired value, but the
code does not actually select the row in the listbox.
Ken
Just out of Curiosity. If I add this, is it then selected?
Thanks
DS

Forms!MenuMaker!ListSections.SetFocus
Forms!MenuMaker!ListSections.Value = Me.TxtSectionID
Forms!MenuMaker!ListSections.Selected = True
 
No. Look back at the code snippet that I provided. Note that the Selected
property requires an argument specifying which row is the one being
selected. Each row has a Selected property value.
 
Ken said:
No. Look back at the code snippet that I provided. Note that the Selected
property requires an argument specifying which row is the one being
selected. Each row has a Selected property value.
Ok Thanks, I was just curious!
DS
 
Back
Top