rits
rits

Reputation: 1544

MS Access Visual Basic for Application: Subscript out of range

I am getting Run-tme error '9': Subscript out of range at this location in code:

interval(i) = i

Piece of code:

Public Sub array_fill(a As Integer, b As Integer)

Dim i As Integer
Dim interval() As Integer
Dim arraySize As Integer

arraySize = b - a

ReDim interval(arraySize)

For i = a To b Step 1
    interval(i) = i
Next i

End Sub

Any idea what is causing this?

Upvotes: 0

Views: 149

Answers (1)

Tim Williams
Tim Williams

Reputation: 166316

If you run your code (for example) using:

Public Sub array_fill 8, 10

Then:

ReDim interval(arraySize)

will size your array 0 to 2. In your loop the first value of i is 8: there is no interval(8)

Exactly how you'd modify your code would depend on what you want to do with the array you're creating.

Upvotes: 1

Related Questions