简体   繁体   中英

How to select first item in MS Access combobox if form is unbound

I tried to preselect the first item in my combobox item with:

Private Sub Form_Load()
    Me.ProjectBox = Me.ProjectBox.ItemData(0)
End Sub

but it says ItemData(0) is null. I think this is because it is being run before the query(Row Source property) to populate the combobox. Is there a way to run it after the combobox is populated

The original code should work fine...

Private Sub Form_Load()
    Me.ProjectBox = Me.ProjectBox.ItemData(0)
End Sub

if ComboBox properties are set correctly in the designer Property Sheet, for example:

RowSource = {query}
ColumnCount = 2     
BoundColumn = 1   
ColumnWidths = 0";1"

Specifically notice that BoundColumn = 1 .

See online docs for BoundColumn . BoundColumn = 0 has a special meaning, namely that the ComboBox's value becomes the list index value (0, 1, 2...) rather than the value of a RowSource column. Note that the index values for other properties like ComboBox.Column(index, row) are 0-based, while ComboBox.BoundColumn starts at 1 for the first RowSource column... a frustrating inconsistency.

You could requery the combobox. That will pause the code until done:

Private Sub Form_Load()
    Me.ProjectBox.Requery
    Me.ProjectBox = Me.ProjectBox.ItemData(0)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM