Subscript out of range

Hello all - I have a functon that was developed by another developer that I was asked to trouble shoot.

In the form, the user clicks a button to add an element to a row, wherever in the reow they choose.

but the script seems to fail if , and I get the subscript out of range error on the line:

NewField(j) = Field(k) 'transfer value from existing field to new field.

Function ExpandField(Field As Variant) As Variant

 '********************************************************************************************************

 '* Created:               January 5, 1998

 '* Author:                 Betsy Thiede (Richmond Information Group) with much help from Gary Shifflett

 '* Purpose:              Adds a new blank element to each field in the dynamic table in prepartion for the insertion of the new element

 '* Parameters:         None

 '* Called by:            GetCurrentFields subroutine

 '********************************************************************************************************     

Dim NewField() As String 'Array to temporarily store the new "expanded" field.

Dim J As Integer 'Index counter for new field

Dim K As Integer 'Index counter for existing field.

Redim NewField(Ubound(field)+1) 



k = 0



For j=0 To Ubound(field) + 1

	If J <> InsertionPoint Then

		NewField(j) = Field(k) 'transfer value from existing field to new field.

		k = k + 1

	Else

		Newfield(j) = "" 'setting the new field's element to blank(k not incremented. J & K are now out of sync by one)

	End If

Next



Expandfield = newfield

End Function

Subject: Subscript out of range

I might be missing something, but what is InsertionPoint equal to? If that hasn’t been set then j will never equal it.

dan

Subject: RE: Subscript out of range

insertion point = 7

Subject: RE: Subscript out of range

The Insertion point # has to fall within the number of items values in the field. In other words it will need to hit the ‘else’ once or it will be out of range. If there are 4 values in the field for example and you use 2 as the Insertion point it should work or if there are 9 values in the field and you use 7 as the insertion point it should work. If there are 5 values in the field and the insertion point is 7 it will be out of range.

Hope this makes sense…

Subject: Subscript out of range

I doubt this is it because I can;'t imagine you’d have this many rows but perhaps the value of j or k is exceeding the limit of Integer (32,767) in which case you could change to Long