More Help with Combobox . . . again

  • Thread starter Thread starter Newbie
  • Start date Start date
N

Newbie

I was given the following code to set the value to the first item of the
combobox:
Me.Combo1.Value = Me.Combo1.ItemData(Abs(Me.Combo1.ColumnHeads))
This works great but how can I set it to the last or 2nd item of the
combobox.

I was also told the following would work

combo1.ListIndex = 1

but I get the message:
Runtime error '7777'
You've used the ListIndex property incorrectly

How can I set the combobox to the item I want it to be?
 
I was given the following code to set the value to the first item of the
combobox:
Me.Combo1.Value = Me.Combo1.ItemData(Abs(Me.Combo1.ColumnHeads))
This works great but how can I set it to the last or 2nd item of the
combobox.

In addition to Dan's suggestion, you can also use:

**For 2nd item in list (watch for line wrap - it's all on one line):

Me.Combo1.Value = Me.Combo1.ItemData(Abs(Me.Combo1.ColumnHeads)+1)

**For last item in list:

Me.Combo1.Value = Me.Combo1.ItemData(Me.Combo1.ListCount - 1)
 
Thanks for this

Do you think you could explain what the
(Abs(Me.Combo1.ColumnHeads))
part of the code is doing please

Thanks again
Al
 
Do you think you could explain what the
(Abs(Me.Combo1.ColumnHeads))
part of the code is doing please

Yes. When you have ColumnHeads turned on, it adds a row to the combo box, but
this row is not data. The row index in a combo box and listbox is zero-based,
meaning that the first row's index is 0, the second is 1, etc.. When ColumnHeads
is turned on, Me.ComboBox.ItemData(0) is the non-data column header, not a data
row, so your first row of data is ItemData(1). "Abs(ColumnHeads)" returns 1 when
ColumnHeads is turned on, so it points to the first row of data;
"Abs(ColumnHeads)" returns 0 when ColumnHeads is turned off, so it still points
to the first row of data.

:-)
 
Thanks for all your help


Bruce M. Thompson said:
Yes. When you have ColumnHeads turned on, it adds a row to the combo box, but
this row is not data. The row index in a combo box and listbox is zero-based,
meaning that the first row's index is 0, the second is 1, etc.. When ColumnHeads
is turned on, Me.ComboBox.ItemData(0) is the non-data column header, not a data
row, so your first row of data is ItemData(1). "Abs(ColumnHeads)" returns 1 when
ColumnHeads is turned on, so it points to the first row of data;
"Abs(ColumnHeads)" returns 0 when ColumnHeads is turned off, so it still points
to the first row of data.

:-)
 
Back
Top