Combo box - setting selected item - Access 97 vba

David Trickett

Registered User.
Local time
Today, 21:52
Joined
Nov 20, 2002
Messages
16
This was covered a while ago, but I can't get the solution to work!
It involved the line

Me.cboPosition = Me.cboPosition.Column(0, N)

But "cboPosition" just won't compile.

I need to select one item in a combo box when the form is loaded depending on a variable already set.

Thus from the calling procedure currentusername="Fred"

The code in Excel vba would look like this:

For thisitem = 0 to Combo1.ListCount
If Combo1.List(thisitem)=currentusername then
Combo1.ListIndex=thisitem
Exit for
End If
Next thisitem

And if Fred is in the list he will be selected.

But of course this doesn't work with Access vba since ListIndex is read only (do MS programmers ever talk to each other?).

Any help would be much appreciated.

Thank you

David Trickett
 
Why not set the default value of your combo box at the open event of your form?

I ran the below on the open event of a test form and it worked fine. You would just need to add the code to determine what value to set the default value for your combo box.

Private Sub Form_Open(Cancel As Integer)
Me!cboTest.DefaultValue = 2
End Sub
 
Thanks jsprenk - but nothing happens when I try this - the combo box stays resolutely blank until I click the dropdown button!

David Trickett
 
OK try this

Try using the .value property rather than the .defaultvalue property.

Private Sub Form_Open(Cancel As Integer)
Me!cboTest.Value = 2

End Sub

If the bound column is a string, be sure to put the value in quotes like below.

Private Sub Form_Open(Cancel As Integer)
Me!cboTest.Value = "test 2"

End Sub
 

Users who are viewing this thread

Back
Top Bottom