Select first item in combo box (1 Viewer)

gazsharpe101

Registered User.
Local time
Today, 20:34
Joined
Oct 23, 2007
Messages
47
Hi everyone,

I have a problem which I assume will have an annoyingly simple solution.

I have a combo box that is populated by an SQL statement, which works ok. What I want to happen is when I open the form that the combo box is on, I would like it to autoselect the first item in the combo box rather than having to click on the arrow and select it.

I have looked around many websites but to no avail. I have tried:
Me.cboName.Value=Me.cboName.Itemdata(0)
and I have also tried setting the list index.

I have been advised that there is a selectedindex property but I can't find it in VBA to use this.

Any help is much appreciated.

Thanks.
Gareth.
 

missinglinq

AWF VIP
Local time
Today, 15:34
Joined
Jun 20, 2003
Messages
6,423
Don't know how you tried using list index, but this works:

Code:
Private Sub Form_Load()
 MyCombo.SetFocus
 MyCombo.ListIndex = 0
 MyCombo.Dropdown
End Sub
 

gdmvet

New member
Local time
Today, 21:34
Joined
Feb 10, 2007
Messages
2
Hi Gazsharpe,

You were close :

Me.cboName = Me.cboName.Itemdata(0)

does the job for me ;)
 

missinglinq

AWF VIP
Local time
Today, 15:34
Joined
Jun 20, 2003
Messages
6,423
Since .Value is the Default Property for comboboxes,

Me.cboName = Me.cboName.Itemdata(0)

and

Me.cboName.Value=Me.cboName.Itemdata(0)

are identical statements!
 

caboone

New member
Local time
Today, 12:34
Joined
Apr 21, 2011
Messages
1
(Note: It looks like this post is old, but still useful for those having this same problem)

Me.cmbxItemsList.SetFocus
Me.cmbxItemsList = Me.cmbxItemsList.ItemData(0)

I wanted to make this happen at Form Load. Therefore, I placed the above code in the form load function. In addition, at combobox change/load I have to load some data into some other fields in that form. I call the following function (previously created) at the bottom of the above code:

cmbxItemsList_Change

And this did the trick for me....
 

missinglinq

AWF VIP
Local time
Today, 15:34
Joined
Jun 20, 2003
Messages
6,423
It is always nice to see old posts 'recycled,' and in order for this to happen, it is very important to give your posts a clear title, such as Select first item in combo box, as this one was titled!

Posts with titles such as Help or Access 2007 are totally useless to someone searching for help from old posts.

Also, since I neglected to point this out in my first post on this thread, the critical thing here is that you must set focus to the Combobox

MyCombo.SetFocus

before using

MyCombo.ListIndex = 0

or

Me.MyCombo = Me.MyCombo.ItemData(0)

to assign a value to it.

Linq ;0)>
 

poduska

Registered User.
Local time
Today, 15:34
Joined
Jul 29, 2008
Messages
13
Thanks everyone, this solves my problem of our upcoming conversion to 2007 ( I know, I know 2010 is out) and still using Send keys, i.e.
Code:
SendKeys "{down}"
SendKeys "{enter}"
Here is what I use with two combo boxes, the second is a function of the first and if there is only one item in the second combo box I do not wish to waste the users time selecting it so it is automatic.
Code:
Private Sub cboProjectFilter_AfterUpdate()
[COLOR=Green]'###########################################################
'Filter bar - "Project" combobox (cboProjectFilter)
'This requeries the "SubProject" Combo box (cboSubProjectFilter) to reflect only the
'available SubProjects for the selected projects.
'############################################################[/COLOR]
Me.cboSubProjectFilter = ""  [COLOR=Green] ' show nothing as criteria has changed.[/COLOR]

Me.cboSubProjectFilter.Requery  [COLOR=Green] 'The criteria of my combobox has changed so requery[/COLOR]

Me.cboSubProjectFilter.SetFocus

        If Me.cboSubProjectFilter.ListCount = 1 Then  [COLOR=Green]  'Auto Select first (only) item[/COLOR]
                Me.cboSubProjectFilter = Me.cboSubProjectFilter.ItemData(0)
        Else  [COLOR=Green] 'User must make  the selection if there are more than one.[/COLOR]
                Me.cboSubProjectFilter = ""
                Me.cboSubProjectFilter.Dropdown
        End If

End Sub
 

gsmisek

New member
Local time
Today, 14:34
Joined
Oct 12, 2012
Messages
2
you must set focus to the Combobox
MyCombo.SetFocus

before using

MyCombo.ListIndex = 0

or

Me.MyCombo = Me.MyCombo.ItemData(0)

Actually, you never need to set focus to a control to use the Value property or ItemData collection.
 

warley

New member
Local time
Today, 16:34
Joined
May 1, 2019
Messages
1
Thanks everyone, this solves my problem of our upcoming conversion to 2007 ( I know, I know 2010 is out) and still using Send keys, i.e.
Code:
SendKeys "{down}"
SendKeys "{enter}"
Here is what I use with two combo boxes, the second is a function of the first and if there is only one item in the second combo box I do not wish to waste the users time selecting it so it is automatic.
Code:
Private Sub cboProjectFilter_AfterUpdate()
[COLOR=Green]'###########################################################
'Filter bar - "Project" combobox (cboProjectFilter)
'This requeries the "SubProject" Combo box (cboSubProjectFilter) to reflect only the
'available SubProjects for the selected projects.
'############################################################[/COLOR]
Me.cboSubProjectFilter = ""  [COLOR=Green] ' show nothing as criteria has changed.[/COLOR]

Me.cboSubProjectFilter.Requery  [COLOR=Green] 'The criteria of my combobox has changed so requery[/COLOR]

Me.cboSubProjectFilter.SetFocus

        If Me.cboSubProjectFilter.ListCount = 1 Then  [COLOR=Green]  'Auto Select first (only) item[/COLOR]
                Me.cboSubProjectFilter = Me.cboSubProjectFilter.ItemData(0)
        Else  [COLOR=Green] 'User must make  the selection if there are more than one.[/COLOR]
                Me.cboSubProjectFilter = ""
                Me.cboSubProjectFilter.Dropdown
        End If

End Sub

Thank you!
 

Users who are viewing this thread

Top Bottom