List Index Propery Behaving Strangely

G

Guest

I have two combo boxes on a form. Their intent is to allow a user to select
a record on the form by choosing a value in either combo box.

I have written code to automatically update one combo box when the other is
changed. Here is the code:

If cboReportNum <> "" Then
If cboReportNum.ListIndex <> cboReportName.ListIndex Then
index = cboReportNum.ListIndex
cboReportName.SetFocus
cboReportName.ListIndex = index
End If
End If

The code attached to the other combo box (cboReportName) is exactly the same
with the names switched appropriately and works to update as planned, but the
above code gives me a "You have used the ListIndex Property Incorrectly"
error.

Also, the exact same code on a different form works properly both ways.

Any ideas why this is not behaving properly?
 
W

Wayne Morgan

The ListIndex property is Read Only, you can't assign a value to it. To set
a combo box to the value in the "x" row, try:

cboReportName = cboReportName.Column(BoundColumn, Index)

The values in the Column property are zero based, so 0 is the first column
or row, 1 is the second, etc. In the line above, BoundColumn is the number
of the Bound Column of the combo box - 1 (if the bound column is the first
column then this value should be 0) and index is the value you had for
index. If it wasn't already zero based, you would have to subtract 1 from
it, but it appears that it is already zero based. Also, "index" is a
reserved word. Try intIndex instead. The "int" would indicate that this is
an Integer variable.
 
M

Marshall Barton

Wayne said:
The ListIndex property is Read Only, you can't assign a value to it.

Wayne, contrary to Help (unless they changed it in A2003),
the ListIndex property is writable when the control has the
focus.
 
M

Marshall Barton

darrep said:
I have two combo boxes on a form. Their intent is to allow a user to select
a record on the form by choosing a value in either combo box.

I have written code to automatically update one combo box when the other is
changed. Here is the code:

If cboReportNum <> "" Then
If cboReportNum.ListIndex <> cboReportName.ListIndex Then
index = cboReportNum.ListIndex
cboReportName.SetFocus
cboReportName.ListIndex = index
End If
End If

The code attached to the other combo box (cboReportName) is exactly the same
with the names switched appropriately and works to update as planned, but the
above code gives me a "You have used the ListIndex Property Incorrectly"
error.


No idea. The code looks good to me. Maybe you forgot to
change one of the names in the other procedure? Also make
sure the two combo boxes have the same number of records in
their RowSources.

If that doesn't help, try Wayne's approach.
 
G

Guest

Thanks for your input. Although the property is writable, as Marsh pointed
out, something about it is buggy. After first posting this issue in the
development phase, I tabled it since it worked where it absolutely needed to.
However, after testing the app on other machines, the code didn't work in
critical situations on some forms although it did on others.

Long story short, Wayne's suggestion to use the Column propery to manipulate
the value works for all situations. THanks for the advice Wayne!

Moral of the story is this: Use the column property of a combo box and not
the ListIndex to maniuplate the value in a combo box!
 

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