Selecting an item in a listbox using VB (1 Viewer)

CrArC

Registered User.
Local time
Today, 13:30
Joined
Oct 7, 2006
Messages
25
Hi there,

If you have a listbox on a form with a series of items in it, either as a value list or from a table/query, how would you go about using visual basic to automatically select (highlight/"click upon") an item in that listbox?

I have a command button on another form which must take the user to a small search form, and automatically highlight/select a specific item in the listbox on that form. This would usually be the user's job but the idea of this button is to find the item they are looking for and set up the form automatically. This has been winding me up for a while now as the only thing I am missing is how to make that selection in the listbox! :)
any help appreciated! Cheers.
 

Steve R.

Retired
Local time
Today, 08:30
Joined
Jul 5, 2006
Messages
4,703
Use the Selected Property.
Code:
Me!Listbox.Selected(4) = True

Here is another sample:
Code:
Public Sub List7Selection()
Rem Checks List7 to see if an item has been selected. If not - the first one is selected.
    i = 0
    j = 0
    RecordCount01 = Me.List7.ListCount
    Do Until i = Me.List7.ListCount
        i = i + 1
        If Me.List7.Selected(i) Then j = i
        Loop
    If j = 0 Then 'No Selected Record Found, which means a record has been deleted.
        Set crst = cdbs.OpenRecordset("select * from projectnumqry where [status]<10", dbOpenDynaset)
        Set Me.Recordset = crst
        [List7].Selected(1) = True        
        End If
End Sub
 

CrArC

Registered User.
Local time
Today, 13:30
Joined
Oct 7, 2006
Messages
25
Fantastic, seems to be doing the trick! Thanks. It's just raised another small issue though: when using this method to change the selection in a listbox, it does update the listbox.value but not for the first item in the list.

For example: an iteration loop that is used to find a record in the listbox based on the listbox.value, when seeking the value of me!listbox.selected(0) will return nothing, but (1) is not the first item on the list! Am I using a stupid method of reading data from the listbox? The value of the listbox corresponds to unique ID numbers for records so it is important the code can read them ALL and check if they match!

Thanks again! :)
 

YevS

Registered User.
Local time
Today, 13:30
Joined
May 23, 2007
Messages
39
"me!listbox.selected(0)" looks a bit wrong.

Index starts at 1
and it should be me.listbox.selected(1) = true or just listbox.selected

You only need to use ! when you are not coding in the form the listbox is located in.

Other than that me.listbox.selected(1) = true should select the first record in the list.
 

CrArC

Registered User.
Local time
Today, 13:30
Joined
Oct 7, 2006
Messages
25
You're right, it was 1! I messed up in the code there- had messageboxes popping up to show me the values but I'd put them in the wrong part of the iteration loop which explains the confusion. It's working just fine now. Thank you :)
 

born2gamble

Registered User.
Local time
Today, 05:30
Joined
Aug 16, 2011
Messages
31
I am new to this, and am having some issues with this. I use access 2010. I think I have a similar situation.
I have ComboBox1, ComboBox2

ComboBox2 is filtered results based on ComboBox1 Selection. (cascading)

I then have a listbox, that based on ComboBox1 and ComboBox2 selections, displays an id. I just want it to auto highlight the selection in the listbox(only one item will be there) after ComboBox2 is updated, rather than the user having to manually highlight the selection.
 
Last edited:

GriffBolt

New member
Local time
Today, 14:30
Joined
Nov 30, 2011
Messages
1
I have a similar problem in relation to a ListBox. To get the event of selecting item by code in the listbox is not a problem.
The challenge I am working on is that after a particular record has been loaded and updated in the database i need to reload the page to get rid of the form data that is being used by Access.
In that case I close the form and load it again within vb and now I want to get back to the record i was editing before by using the select, as in clicking the record, and loading the data used by the different subforms.
I do hope someone can help me with this particular challenge.
 

Thales750

Formerly Jsanders
Local time
Today, 08:30
Joined
Dec 20, 2007
Messages
2,149
This is from FMS.
FMS
Neat, Compact, and well you know, it works.

Code:
If IsNull(Me.lstBox) Then
  Me.lstBox = Me.lstBox.ItemData(0)
End If
 

Mike Krailo

Well-known member
Local time
Today, 08:30
Joined
Mar 28, 2020
Messages
1,044
Thales750, you do realize that you just responded to a post from 2011.
 

Thales750

Formerly Jsanders
Local time
Today, 08:30
Joined
Dec 20, 2007
Messages
2,149
Thales750, you do realize that you just responded to a post from 2011.
I do.
Google found it, but it has all kinds of bogus extra code. I thought I would save someone the trouble. I've been using that snippet forever, I just couldn't find it in my library so I googled it.
Now we have a better version on file.
 

Users who are viewing this thread

Top Bottom