i am writing a program but when i run it the program works and displays what i want once i have entered the info in but as soon as i have entered the info it displays an error. it says its to do with this line "lstDisplay.AddItem names(index) + Str$(marks(index))"
this is all of my code
Option ExplicitDim position As IntegerDim names(10) As StringDim marks(10) As String
Private Sub cmdend_Click()
End
End Sub
Private Sub cmdGo_Click()
names(position) = txtInputmarks(position) = txtmarkposition = position + 1txtPosition = position
If position > 10 Then MsgBox "Full", vbCriticalEnd If
Dim index As Integer
lstDisplay.Clear
For index = 1 To position lstDisplay.AddItem names(index) + Str$(marks(index))Next
Private Sub Form_Load()position = 1txtPosition = positionEnd Sub
I'm guessing after you enter in a name or something, you increment your position by 1.
Then later you go onto adding items into some sort of list display container.
Now what I think is a problem is as follows. Because of the way you've implemented your position, it will always be + 1, everytime you get a new name. Now, I'm not sure, but I'm guessing VB initializes integers to 0. So say you start at 0 and you add 3 people, your count will be 0[first name] pos+1, 1[second name], pos+1, 2[third name], pos+1. At the end of this, position = 3.
Now later, you start to add items, and you index from 1 - position, for this case, it's 3.
That means you are indexing names(1), names(2), and names(3).. well, names(3) doesn't exist.
You might want to try doing your for from 0 -> pos - 1
Or use an iterator.
Note: IF VB indexes from 1->n and NOT 0->n-1, then you can fix this by incrementing position before you assign the text into the arrays.