More Help with Combobox . . . again

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?
 
D

Dan Artuso

Meant to also that the combo has to have the
focus to use the ListIndex property.
 
B

Bruce M. Thompson

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)
 
N

Newbie

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
 
B

Bruce M. Thompson

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.

:)
 
N

Newbie

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.

:)
 

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