0

I have a text file which is used to send invoices. Example below, the :IIII4444: comes before each new invoice, the 0000 line is the invoice number line, the 0001 line for some reason is always blank but the 0001 still needs to be there, and the 0002 line are the itemized items that make up the invoice. In VB6 I would like to, in a windows form, display the invoices one by one with a next and previous button. Any help with this would be appreciated.

:IIII4444:
0000 InvoiceNumber
0001
000200001 46.00     HR215.00
000200001 53.00     HR215.00
000200001 53.00     HR215.00
:IIII4444:
0000 InvoiceNumber
0001
000200001 40.00     HR48.96
000200001 40.00     HR48.96
:IIII4444:
0000 InvoiceNumber
0001
000200001 18.00     HR257.50
000200001 16.00     HR257.50
000200002 4.50      HR284.00
000200002 2.00      HR284.00
000200003 0.50      HR257.50
000200003 4.00      HR257.50
000200004 1.00      HR309.00
000200007 1.50      HR284.00
000200007 3.00      HR284.00
000200008 3.00      HR255.60
000200008 3.00      HR255.60
000200008 2.50      HR255.60
000200008 5.00      HR255.60
000200009 3.25      HR257.50
000200010 3.40      HR231.75
000200010 1.90      HR231.75
000200013 2.00      HR284.00
000200013 2.00      HR284.00
000200014 1.00      HR293.94
000200014 16.50     HR293.94
000200015 2.75      HR257.50
000200015 6.75      HR257.50
000200017 1.00      HR284.00
000200017 1.00      HR284.00
000200018 3.00      HR309.00
000200018 2.00      HR309.00
000200019 6.00      HR255.60
000200019 6.00      HR255.60
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Pat
  • 1
  • 1
  • It is not clear what kind of “help” you are looking for. Do you want someone to write the program for you? That is not what Stack Overflow is for; try Elance. Or are you writing a program to do this? Show us your program, tell us what it does, tell us what you want to to do, explain what is different between what you have and what you want. – Dour High Arch May 27 '14 at 21:03
  • I am sorry. No I do not want someone to write the program for me. I wanted to see if someone could show me how to get each line group into an array, collection or whatever will work, so I am able to then display them. Sorry forgot to add that. – Pat May 27 '14 at 21:06
  • Please consult [Read data from a text file using VB6](http://stackoverflow.com/questions/2873830/). – Dour High Arch May 27 '14 at 21:23

2 Answers2

0

You can read the entire file into 1 string, and then use the Split() function to split it into an array :

Private Sub ReadAndSplitFile(strFile As String)
  Dim intFile As Integer
  Dim strData As String
  Dim strLine() As String
  'read in the entire file
  intFile = FreeFile
  Open strFile For Input As #intFile
    strData = Input(LOF(intFile), #intFile)
  Close #intFile
  'split the lines in the file into an array
  strLine = Split(strData, vbCrLf)
End Sub
Hrqls
  • 2,944
  • 4
  • 34
  • 54
0

This is a line-based file, so I would open the file for standard line-based input, and iterate through it until there are no more lines:

Dim iFileNo As Integer
Dim sLine   As String
Dim Invoice As <Whatever>

' Retrieve the next file number.
iFileNo = FreeFile

Open "Invoice.txt" For Input As #iFileNo

Do Until Eof(iFileNo)
    Line Input #iFileNo, sLine
    ProcessInvoice sLine, Invoice
Loop
ProcessInvoice "", Invoice
Close #iFileNo

Pseudo-code for ProcessInvoice:

If the line is equal to ""
    If we are already processing an Invoice
        Finish with that Invoice
Else If the line is equal to ":IIII4444:"
    If we are already processing an Invoice
        Finish with that Invoice
    Start new invoice.
Else If the line starts with "0000"
    Save the invoice number (everything after "0000 ")
Else If the line starts with "0001"
    Do nothing
Else If the line starts with "0002"
    Call "ProcessInvoiceEntry" (everything after "00002")
    Save the invoice entry

ProcessInvoiceEntry

' The offsets are 
'
' 0        1         2         3
' 123456789012345678901234567890
'
' 00001 18.00     HR257.50
' ^    ^^        ^^
' +----++--------++------------->

Pull out each chunk from the string, and return each field as a separate string or number.

(Note that Mid$() is the function to copy a substring from a string. Use RTrim$() to get rid of any excess spaces.)

The previous code assumes that you load all the invoices at once, saving them in some data structure - an Invoice and an InvoiceEntry class, for instance. Another way you could do this would be to iterate through the entire file, only looking at the invoice numbers, and create a list with just the Invoice numbers, and the line number associated with it. This way you could load the invoices into the form, on demand.

Mark Bertenshaw
  • 5,594
  • 2
  • 27
  • 40