I have the following code and I want to print out each customer's custnum and grpnum. For some reason it prints out each line several times before moving to the next number. Can some one help me out?
Private Sub Command1_Click()
Dim MyString As String, CustNum As String, GrpNum As String, Status As String
Dim OutputFile As String, OutFile As Long, InFile As Long
Dim PrintToFile As Boolean
InFile = FreeFile
Open "C:\Documents and Settings\mike\rpt_ccu.txt" For Input As InFile
OutFile = FreeFile
Open "C:\Documents and Settings\mike\cust-info.csv" For Output As OutFile
Write #OutFile, "CustNum", "GrpNum"
Do While Not EOF(InFile)
Line Input #InFile, MyString
If InStr(MyString, "Customer Number") > 0 Then
CustNum = Trim$(Mid$(MyString, 23, 6))
PrintToFile = True
End If
If InStr(MyString, "Group Number") > 0 Then
GrpNum = Trim$(Mid$(MyString, 47, 4))
PrintToFile = True
End If
If InStr(MyString, "Status [") > 0 Then
Status = Trim$(Mid$(MyString, 91, 1))
End If
If PrintToFile = True Then
Print #OutFile, CustNum; ","; GrpNum
End If
Loop
Close OutFile, InFile
Print "done!"
End Sub
Expected Output:
CustNum,GrpNum
1176,179
2173,255
My output:
CustNum,GrpNum
1176,179
1176,179
1176,179
1176,179
...
2173,255
2173,255
2173,255
2173,255