Selecting a combo box item programatically

I

Ibrahim Mesbah

I implemented a wizard that dynamically updates the sub forms when Next is
clicked. I would like to automatically select a certain item in a combo box
in one of the sub forms. The combo box is filtered, so it only has one item.
Is there a way to select the first item programatically??? The subform is
in DataEntry mode.

I'd think there would be a property I could set, like SelectedIndex or
something, but there isn't.

Thanks in advance.
 
B

Brendan Reynolds

Me.Combo2 = Me.Combo2.ItemData(0)

Where 'Combo2' is the name of your combo box. The ItemData collection is
zero-based, so ItemData(0) is the first item in the list, ItemData(1) is the
second item, etc.
 
I

Ibrahim Mesbah

Thank you. That worked, but still having problems. I was doing this logic
in the OnLoad function, and it didn't work. I'm guessing it's because the
combo was yet to be calculated. The row source for this combo is a sql
statement. So I moved the code to the OnCurrent, and that seemed to work,
except now the focus is not at the first field. I figured, ok, i'll just set
the focus to the first field, which is a requireed one, using SetFocus after
the combo box operation, but that didn't work. When I click in the text box
manually, I get an error msg from Access saying the field cannot be null (the
first one). I click on ok, then click in the field, and it's good. I
figured I can get around this by turning the warnings off, but that didn't
help... The more I use access the more I hate it - documentation sucks too..


Thanks again
 
J

John B

If you want to trigger AfterUpdate event, add:

Me.Combo2.Requery
after Me.Combo2 = Me.Combo2.ItemData(0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top