Subscript out of range error

Hello!I am lost with this one.

I’m getting a “Subscript out of range error” when running the following code

Thanks in advance for the help.

Sub doImport(doc As NotesDocument)

Dim myItem As NotesItem

Dim s As New NotesSession

Dim targetDb As NotesDatabase



Dim fileNum As Integer

Dim file As String

Dim text As String

Dim pos, oldPos As Long

Dim counter As Integer

Dim dummy As String

Dim impValues() As String	

Dim fSep As String*1

Dim stringSep As String*1

Dim labels1st As Integer, lineNum As Integer

Dim removeStringSep As Integer



'get some init values

' Assign the lowest available file number to fileNum.

fileNum = Freefile()

file = doc.sn_impFile(0)

fSep = doc.sn_fieldSep(0)

stringSep = doc.sn_stringSep(0)

If Isnumeric(doc.sn_hasLabels(0)) Then

	If Cint(doc.sn_hasLabels(0)) = 1 Then

		labels1st = True

	Else

		labels1st = False

	End If

Else

	labels1st = False

End If



Set targetDB = s.GetDatabase(doc.sn_server(0), doc.sn_database(0))

If Not targetDB.IsOpen() Then

	Call targetDB.Open("","")

End If



If Isnumeric(doc.sn_doStringTrim(0)) Then

	If Cint(doc.sn_doStringTrim(0)) = 1 Then

		removeStringSep = True

	Else

		removeStringSep = False

	End If

Else

	removeStringSep = False

End If





Set myItem = doc.GetFirstItem("tmpFileValues")



Redim impValues(doc.tmpValueCount(0)) As String



Open file For Input As fileNum%



If labels1st Then

	'read first line if labels

	Line Input #fileNum, text

End If

'read the rest of the file

Do Until Eof(fileNum)

	Line Input #fileNum, text

	lineNum = lineNum+1

	Print "Importing #: ", lineNum, text

	pos = 0

	counter = 0

	oldPos = 1

	pos = Instr(oldPos, text, fSep)

	While Not pos = 0 

		dummy = Mid$(text, oldPos, pos-oldPos)

		counter = counter+1

		If removeStringSep Then

			impValues(counter) = stringTrim(dummy, stringSep)  >>>>> it stops here

		Else

			impValues(counter) = dummy							

		End If

		oldPos = pos+1

		pos = Instr(oldPos, text, fSep)

		If ((pos = 0) And (oldPos < Len(text))) Then

			dummy = Mid$(text, oldPos,  Len(text)-oldPos+1)

			counter = counter+1

			If removeStringSep Then

				impValues(counter) = stringTrim(dummy, stringSep)

			Else

				impValues(counter) = dummy							

			End If

		End If

	Wend

	'now we have the values from the current line - let's take care about the mapping

	Call doAction(impValues, doc, targetDB)

	

Loop	



Close fileNum%



'run Agent in Target Database ?

If doc.sn_runAgent(0) = "1" Then

	Dim runAgent As NotesAgent

	Set runAgent = targetDb.GetAgent(doc.sn_agent(0))

	runAgent.Run

End If

End Sub

Subject: Subscript out of range error

Check if tmpValueCount of the document (Redim impValues(doc.tmpValueCount(0)) As String) is null. If so make sure it is not empty. If not empty, try to preserve the values of the array as follows within the loop.

If counter = 0 then

redim impValues(counter)

impvalues(counter) = “value”

else

ReDim Preserve impValues(counter)

impvalues(counter) = “value1”

end if