Selecting a ComboBox row by Index (1 Viewer)

dungstar

Registered User.
Local time
Today, 20:49
Joined
Mar 13, 2002
Messages
72
I have combobox for which I wish to select a value based on an designated index integer N. I tried the following code but it says that I am not using the ListIndex property correctly.

Me.cboPosition.ListIndex = N

This works for my listbox; I'm trying to get the same effect:
Me.lstPosition.Selected(N) = True

Thanks!
 

Jon K

Registered User.
Local time
Today, 20:49
Joined
May 22, 2002
Messages
2,209
The ListIndex property is read-only.


Try this instead:-
Me.cboPosition = Me.cboPosition.Column(0, N)


which should select (i.e. display the contents of) the first column N+1th row of the combo box (Note: the column and row indexes are zero based.)
 

dungstar

Registered User.
Local time
Today, 20:49
Joined
Mar 13, 2002
Messages
72
Zero-based row index

It works! Thank you very much, Jon!

Am I correct in saying the following for comboboxes, the row indexes being, zero-based:

1) If column header is shown, then Row index 0 = the column header (or if used in the aforementioned line of code), and the value would be null.
2) If column header is not displayed then the row index for the combobox is the first "value" row.
3) This is also true for listboxes.
 

Jon K

Registered User.
Local time
Today, 20:49
Joined
May 22, 2002
Messages
2,209
Normally row index 0 is the first row of "values" in a combo box or list box.


However, if the column header is shown, row index 0 refers to the column header row.

So Me.cboBoxORlstBox.Column(n,0) would return the contents of the column header. Its value would not be null.

And Me.cboBox = Me.cboBox.Column(n,0) would display the column header in the combo box. But we can't use Me.lstBox.Selected(0) = True to select the column header row in a list box.


In VBA, we can test for the existence of column header with:-

IF Me.cboBoxORlstBox.ColumnHeads then ...
 

Users who are viewing this thread

Top Bottom