CrArC
11-02-2007, 07:04 AM
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.
11-02-2007, 07:49 AM
Use the Selected Property. Me!Listbox.Selected(4) = True
Here is another sample:
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
11-11-2007, 04:06 AM
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! :)
"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
11-12-2007, 02:51 AM
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
11-29-2011, 10:29 PM
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.
GriffBolt
11-30-2011, 06:56 AM
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.