Computing.Net > Forums > Programming > VB 6 - Text Files

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB 6 - Text Files

Reply to Message Icon

Original Message
Name: oldglory
Date: March 12, 2004 at 08:34:06 Pacific
Subject: VB 6 - Text Files
OS: XP
CPU/Ram: 2GHz/256MB
Comment:

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


Report Offensive Message For Removal


Response Number 1
Name: zgb
Date: March 12, 2004 at 09:50:50 Pacific
Reply: (edit)

In your loop, you need to set PrintToFile to false before you read the next line of text from InFIle. You could put it after the line Print #Outfile, CustNum.......
Add the line PrintToFile=False



Report Offensive Follow Up For Removal

Response Number 2
Name: oldglory
Date: March 12, 2004 at 10:45:51 Pacific
Reply: (edit)

You're a genious! Thanks a million.


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software