Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net Using text files to create incremental numbers not working

Hi
I am testingthe function below that I am trying to create incremental invoice numbers with, ie 1,2,3,4 etc but for some reason the results are 1, 49, 54, 54, 54 etc

Sub Test
        MsgBox(oGet_Invoice_Number)
End Sub

    Function oGet_Invoice_Number() As Long
        Try
            Dim oLastInvoiceNumber As Long
            Dim oInvoiceNumber As Long
            If System.IO.Directory.Exists("c:\Program Files\KISS_Accounting") = False Then
                System.IO.Directory.CreateDirectory("c:\Program Files\KISS_Accounting")
            End If
            Dim FILE_NAME As String = "c:\Program Files\KISS_Accounting\Invoice_Number.txt"
            'So first see if text file is there - if not write the initial file with 1 as number
            If System.IO.File.Exists(FILE_NAME) = False Then
                Dim oWriter As New System.IO.StreamWriter(FILE_NAME)
                oWriter.Write("1")
                oWriter.Close()
                oGet_Invoice_Number = 1
                Exit Function
            Else
                Dim oReader As New System.IO.StreamReader(FILE_NAME)
                Dim oReadLine As String = oReader.Read()
                oLastInvoiceNumber = CLng(oReadLine)
                oReader.Close()
                oInvoiceNumber = oLastInvoiceNumber + 1
                Dim oWriter As New System.IO.StreamWriter(FILE_NAME)
                oWriter.Write(CStr(oInvoiceNumber))
                oWriter.Close()
                oGet_Invoice_Number = oInvoiceNumber
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks. Well spotted!
Use
Dim oReadLine As String = oReader.ReadLine()

Open in new window

instead of
Dim oReadLine As String = oReader.Read()

Open in new window

Hi MacroShadow. Sorry I accepted before seeing your post. Would have split the points