Hello. I would be most grateful if someone would shed some light on the following problems.
I have executed a code fragment on two PCs running VB 6 SP6 and with three sets of input data and get the same results which are:
For an input text file with 29 lines:
Three dynamic arrays re-dimensioned in the same Do While Not EOF loop have lengths of 0 to 29, 0 to 30 and 0 to 30 elements respectively.
I have tried with text files with 9 and 3 records and got similar results. I.e. the first array has 1 more element than the input file has records and the other two arrays have 2 more element than the input file has records.
In the input files I have left the cursor on the same line as the last record. I have also pressed CR to take the cursor to the next line. They made no difference to the results.
My questions are:
1. Why are the arrays not the same length?
2. Why are extra elements being created in the arrays ?
3. Why do I need the line with ReDim arrKilograms(0) before I can use
ReDim Preserve arrKilograms(UBound(arrDate) + 1). I get an error if omit it.
Form declarations are:
Dim arrDate() As String
Dim arrPounds() As String
Dim arrKilograms() As String
Dim strData As String
Dim fileName As String
Dim FILEin As Integer
Dim dblIndex As Integer
Private Sub PopulateArrays()
On Error GoTo ErrorHandler14
Open App.Path & "\" & fileName For Input As #1
dblIndex = 0
ReDim arrDate(0)
ReDim arrPounds(0)
ReDim arrKilograms(0)
Do While Not EOF(1)
ReDim Preserve arrDate(UBound(arrDate) + 1)
ReDim Preserve arrPounds(UBound(arrDate) + 1)
ReDim Preserve arrKilograms(UBound(arrDate) + 1)
Line Input #1, strData
arrDate(dblIndex) = Mid(strData, 1, 9)
arrPounds(dblIndex) = Mid(strData, 23, 3)
arrKilograms(dblIndex) = Mid(strData, 20, 2)
dblIndex = dblIndex + 1
Loop
Close #1
Exit Sub
ErrorHandler14:
Close #1
MsgBox (" ErrorHandler14 Error in Sub PopulateXandYArrays() has been handled ")
' dblIndex = dblIndex
End
End Sub
INPUT DATA all elements are separated by a tab; except the words date and stone has two tabs
date Stones lbs onz kg Total lbs
01-Oct-09 10 00 00 64 140
01-Nov-09 10 07 00 67 147
I have executed a code fragment on two PCs running VB 6 SP6 and with three sets of input data and get the same results which are:
For an input text file with 29 lines:
Three dynamic arrays re-dimensioned in the same Do While Not EOF loop have lengths of 0 to 29, 0 to 30 and 0 to 30 elements respectively.
I have tried with text files with 9 and 3 records and got similar results. I.e. the first array has 1 more element than the input file has records and the other two arrays have 2 more element than the input file has records.
In the input files I have left the cursor on the same line as the last record. I have also pressed CR to take the cursor to the next line. They made no difference to the results.
My questions are:
1. Why are the arrays not the same length?
2. Why are extra elements being created in the arrays ?
3. Why do I need the line with ReDim arrKilograms(0) before I can use
ReDim Preserve arrKilograms(UBound(arrDate) + 1). I get an error if omit it.
Form declarations are:
Dim arrDate() As String
Dim arrPounds() As String
Dim arrKilograms() As String
Dim strData As String
Dim fileName As String
Dim FILEin As Integer
Dim dblIndex As Integer
Private Sub PopulateArrays()
On Error GoTo ErrorHandler14
Open App.Path & "\" & fileName For Input As #1
dblIndex = 0
ReDim arrDate(0)
ReDim arrPounds(0)
ReDim arrKilograms(0)
Do While Not EOF(1)
ReDim Preserve arrDate(UBound(arrDate) + 1)
ReDim Preserve arrPounds(UBound(arrDate) + 1)
ReDim Preserve arrKilograms(UBound(arrDate) + 1)
Line Input #1, strData
arrDate(dblIndex) = Mid(strData, 1, 9)
arrPounds(dblIndex) = Mid(strData, 23, 3)
arrKilograms(dblIndex) = Mid(strData, 20, 2)
dblIndex = dblIndex + 1
Loop
Close #1
Exit Sub
ErrorHandler14:
Close #1
MsgBox (" ErrorHandler14 Error in Sub PopulateXandYArrays() has been handled ")
' dblIndex = dblIndex
End
End Sub
INPUT DATA all elements are separated by a tab; except the words date and stone has two tabs
date Stones lbs onz kg Total lbs
01-Oct-09 10 00 00 64 140
01-Nov-09 10 07 00 67 147