Set selected item of combobox (access)
The subject says it all really.
I want to set the selected item in a combobox from code. Using VBA in access.
Basically, I want it to pre-select the first option on load, and I want to be able to move the selected item down one item when needed.
Thanks
Re: Set selected item of combobox (access)
Use the .SelectedIndex property. 0 is the first item.
Re: Set selected item of combobox (access)
The control doesnt seem to have that property under access. :confused:
Re: Set selected item of combobox (access)
Sorry, I was still in VB.NET mode. Use the .ListIndex property. Same - 0 for first item and -1 for no selection.
Re: Set selected item of combobox (access)
Well, thats what I thought. But if I try it I get runtime error '7777' - You've used the litstindex property incorrectly.
Tried setting it to -1, 0 and 1 but same error every time.
Any ideas? I'm sure I've done this before.
Re: Set selected item of combobox (access)
Post the code procedure in concern.
Re: Set selected item of combobox (access)
Code:
lstbox.Selected(<itemnumber>) = true
Re: Set selected item of combobox (access)
Well ... starting feel pretty dumb right about now.
Basically, I added a combobox to an access form, named it newCombo and bound it to a recordset.
Then I went into the code for Private Sub Form_Load() and am looking through the autocomplete list of methods and properties.
So, I type newCombo. and if I look through the list, I cant find .selected, it doesn't have .selectedIndex, it does have .listIndex but I cant seem to use it.
If I try to use them anyway, they throw errors.
Surely there is a way to do this, but I just cant seem to find it.
Any more help here would be great. :)
1 Attachment(s)
Re: Set selected item of combobox (access)
Heres a working example db for you.
Re: Set selected item of combobox (access)
Apologies - yes I was looking at a listbox not a combo box (duuh).
Listindex (as RobDog has posted) should work fine, in normal instances, however you've bound it so this may affect things. As I don't use bound controls (hate 'em) I cannot advise on this; except to say you should experiment with as many different ways/methods as you can then you can decide which is better.
Uh... second thought - if its bound why are you trying to change the selected item... surely it updates as you change records?
Re: Set selected item of combobox (access)
Quote:
Originally Posted by Ecniv
Apologies - yes I was looking at a listbox not a combo box (duuh).
Listindex (as RobDog has posted) should work fine, in normal instances, however you've bound it so this may affect things. As I don't use bound controls (hate 'em) I cannot advise on this; except to say you should experiment with as many different ways/methods as you can then you can decide which is better.
Uh... second thought - if its bound why are you trying to change the selected item... surely it updates as you change records?
Good point Ecniv. If its a bound control you can navigate the rs so its displaying the record or item you desire.
Re: Set selected item of combobox (access)
I just had the same problem in Access 2007. My problem turned out to be that I did not setfocus to the control I was changing.
I am populating two list or combo boxes with the same info. After you have chosen one I want to programmatically choose the next one to avoid time wasted for the user. Neither worked. Most of MS documents I came across tonight said the Listindex was "Read Only" only 2 stated it was read/write.
1. In any case my problem was I did not setfocus to the control I was changing. While others I have populated with info I did not have to setfocus too.
2. Just in case, for those that do not know. You have to make sure the item is selected and not just scrolling to it.
With my number of years experience I still dont know the pattern or rule for setfocus. Any documentation or rules as to when you have to and should setfocus? Why I ask is I populate a number of textboxes during this code and setfocus never came to mind being the other objects filled in.
Thank You.
Re: Set selected item of combobox (access)
It's kind of a pain in the butt in VBA.
You have to set either the Value or DefaultValue of the ComboBox equal to the value of the first item in the list.
Re: Set selected item of combobox (access)
Hi All,
Using Access 2007 I just did this for a combobox to preselect a certain value (Values are from a query):
Code:
Me.comboIssue_Status = 1
Remember if the query results are sorted then it may not be clear what the index of the item is. I found it easier to select the item, then press any button having set a breakpoint and check the value of the combobox which in my case was 1 (which was the 3rd item in the list as the list was sorted)
Kristian
Re: Set selected item of combobox (access)
cboYourName.value = cboYourName.Listindex(0) 'set combobox to top of list