ListBox with MultiSelect

I had a listbox (single select) containing user information (noUser, name, stats) and I was using a ComboBox containing user info (noUser, name). So by selecting the name in the combo, I was highlighting the user in my listbox with the following code:

Forms![listbox].value = Forms![Combo].value  'Easy

But now I've changed my Listbox (single select) for a Listbox multiSelect, and it doesn't work since the .value in multiselect is always Null.

Is there an easy way to do that? I know I could have my data out of the listbox with .column(0) instead of .value, but to highlight the row containing noUser = X... I can't figure that out.

Any help would be appreciated, thanks
Jeff

jeanfrancoismelanconAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
dovholukConnect With a Mentor Commented:
sorry. i can't find a more suitable method... here's what i use:

Function FindIndexValue(ctrl As Control, ValueToFind As Variant) As Long
Dim i As Long

For i = 0 To ctrl.ListCount - 1
     If ctrl.ItemData(i) = ValueToFind Then
          FindIndexValue = i
          Exit Function
     End If
Next i
FindIndexValue = -1
End Function

dovholuk
0
 
dovholukCommented:
if you are trying to highlight a particular row, use this syntax:

Forms!YourForm!YourListBox.Selected(IndexValue) = True

i usually create a subroutine such as FindIndexValue for the listbox that will loop through and determine the index number if you don't know it... know what i mean?

cheers.

dovholuk
0
 
jeanfrancoismelanconAuthor Commented:
This is what I wanted to know. So there's no subroutine to do this for me... I'll have to create one myself?
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
dovholukCommented:
there's not a good one that i know of. maybe someone else can inform you of an access builtin feature i'm not aware of.

i use a for next loop and compare the values to the desired value, then (using the for next variable) when i find a match i know what index to "select".

i've tried using such things like ...SomeListBox("SomeValueIWantToFind").index but i've never gotten it to work...

i'll poke around a bit though as i'm intrigued again...

dovholuk
0
 
PsychoDazeyCommented:
I have this subroutine that chooses one or more items depending on which check box you click on, I'm sure you could modify it to do what you need:
Dim i As Integer
Dim x As Integer
Dim strSearchFor As String
 x = 0
 If Me!Check4 = -1 Then
    strSearchFor = "CT"
 ElseIf Me!Check5 = -1 Then
    strSearchFor = "New York"
    For i = 1 To List1.ListCount - 1
        If List1.ItemData(x) = strSearchFor Then
            List1.Selected(x) = True
    'Exit For
        End If
        x = x + 1
    Next
End If
0
 
jeanfrancoismelanconAuthor Commented:
I tried your code, it works for what it is made for, but doesn't for what I need.

I want to make it highlight my row with the noUser I have, but as if I clicked on it in my listbox (so it would unselect every other rows, and become the "current value" of the multiselect listbox).
0
 
jeanfrancoismelanconAuthor Commented:
This is a listbox multiselect extended in Access 97.
0
 
dcomCommented:
If the list box and the combo box contain the same data in the same order????? Give this a try.

Public OldValue as Long

Private Sub Combo_AfterUpdate()
  Me.ListBox.Selected(OldValue) = False
  Me.ListBox.Selected(Me.Combo.ListIndex) = True
  OldValue = Me.Combo.ListIndex
End Sub

The middle line is the actual highlighter.  The others are to un-highlight the previous value in the list.
0
 
PsychoDazeyCommented:
Hmmm... I would start by trying this in the after update for your combo box:
Dim i As Integer
Dim x As Integer
Dim strSearchFor As String
x = 0
   strSearchFor = Me.[ComboBoxName].Text
   For i = 1 To List1.ListCount - 1
       If List1.ItemData(x) = strSearchFor Then
           List1.Selected(x) = True
   'Exit For
       End If
       x = x + 1
   Next
End If
0
 
jeanfrancoismelanconAuthor Commented:
This works perfectly, I don't know why I said it didn't work... it works.

Thanks a lot
0
 
PsychoDazeyCommented:
Problem solved then...good work dovholuk (as usual).
0
 
dovholukCommented:
right on. glad to help.

props and thanks to you psycho. have you officially made kpro status? if so congrats...

dovholuk
0
 
PsychoDazeyCommented:
Yes, ihave.  I appreciate the congrats.  I actually learn alot from you as I go through the questions and read your postings.  Most of the time I realize I have been doing things the long way!  Thanks and keep up the good work.  Good luck to you also jean, I hope I can assist you in the future!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.