Run-time error '381'" Invalid Property array index

I am in need of assistance on an error that I'm getting.  I have created a form that when load goes and read an excel spreadsheet and populate a combo box.  If the user clicks on the combo box, it will populate the text box base on the specified selection.

The error that I'm getting is "Run-time error '381': Invalid property array index."  This error will only occur if I select the first selection from the combo box.  Anything else seems excluding the first selection from the combo box seems to work fine.

Below is my code:

rsData.Open "Select * From MODES", conn, adOpenDynamic, adLockOptimistic

Do Until rsData.EOF
    If Not IsNull(rsData.Fields(0)) Then
    cboModes1.AddItem rsData.Fields(0)
    cboModes1.ItemData(rsData.AbsolutePosition - 1) = rsData.AbsolutePosition
    End If
    rsData.MoveNext
Loop



Private Sub cboModes1_Click()
    rsData.MoveFirst
    rsData.Move cboModes1.ItemData(cboModes1.ListIndex - 1)         ------------------------->  Here is where i get the error if I select the first selection from the list.
    txtModes1.text = rsData.Fields(1)
End Sub
holemaniaAsked:
Who is Participating?
 
Ryan ChongCommented:
ListIndex is Zero based, so you should try like this instead:

rsData.Move cboModes1.ItemData(cboModes1.ListIndex)  

?
0
 
holemaniaAuthor Commented:
If I set it to as you mentioned above, it does not error out.  However instead of getting Listindex 0 to show i'm getting listindex1 for my description.  

For example A1 and B1 should tied to each other and listed as Listindex 0, but now when I select from the combo box the value of A1, it displays value of B2 instead of B1.  \

Shutdown --------All valve closed and lights off.
Run----------------Chamber set to a point.

If "Shutdown" is selected, i get "Chamber set to a point".  With my example, it corresponds however my first selection gives me a run time error.  I am assuming it might have to do with my form load cboModes1.ItemData(rsData.AbsolutePosition - 1) = rsData.AbsolutePosition.  I thought that if 1-1 should equal 0, but for some strange reason that line is giving me index of 1.
0
 
holemaniaAuthor Commented:
Also as a side note if I select the last value "Run" from the combo box, it gives me an error "Run-time error '94': Invalid use of Null".  It seems that it is skiping an index.  
0
 
holemaniaAuthor Commented:
I kind of figure out a work around.  WIth your example I was able to select the first item on the list and with mine I was able to select last item on the list.  So I combine both and it seems to work.

rsData.MoveFirst
If cboModes1.ListIndex = 0 Then
     rsData.Move cboModes1.ItemData(cboModes1.ListIndex)
Else
    rsData.Move cboModes1.ItemData(cboModes1.ListIndex - 1)
End IF
txtModes1.text = rsData.Fields(1)


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.