Solved

ListBox with MultiSelect

Posted on 2001-08-10
13
460 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:jeanfrancoismelancon
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 8

Expert Comment

by:dovholuk
ID: 6372907
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
 

Author Comment

by:jeanfrancoismelancon
ID: 6372966
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
 
LVL 8

Expert Comment

by:dovholuk
ID: 6373000
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
 
LVL 8

Accepted Solution

by:
dovholuk earned 100 total points
ID: 6373034
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
 
LVL 6

Expert Comment

by:PsychoDazey
ID: 6373044
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
 

Author Comment

by:jeanfrancoismelancon
ID: 6373845
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jeanfrancoismelancon
ID: 6373855
This is a listbox multiselect extended in Access 97.
0
 

Expert Comment

by:dcom
ID: 6373857
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
 
LVL 6

Expert Comment

by:PsychoDazey
ID: 6373860
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
 

Author Comment

by:jeanfrancoismelancon
ID: 6373934
This works perfectly, I don't know why I said it didn't work... it works.

Thanks a lot
0
 
LVL 6

Expert Comment

by:PsychoDazey
ID: 6373942
Problem solved then...good work dovholuk (as usual).
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6374136
right on. glad to help.

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

dovholuk
0
 
LVL 6

Expert Comment

by:PsychoDazey
ID: 6374268
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now