-2

On Stack Overflow, I found the question Generate a receipt number in this range.

In answers (from Adam Maras) I find this code (very interesting for me):

Private Const FirstReceiptNumber As String = "GA00000"

Public Function GenerateReceiptNumber(ByVal lastNumber As String) As String

    If lastNumber.Length <> 7 Then
        Throw New ArgumentException("Incorrect length", "lastNumber")
    End If

    If lastNumber.StartsWith("G") = False Then
        Throw New ArgumentException("Incorrect start character", "lastNumber")
    End If

    Dim letterPortion As Char = lastNumber.Chars(1)

    If letterPortion < "A"c Or letterPortion > "Z"c Then
        Throw New ArgumentException("Non-letter second character", "lastNumber")
    End If

    If letterPortion = "I"c Or letterPortion = "O"c Then
        Throw New ArgumentException("Invalid second character", "lastNumber")
    End If

    Dim numericPortionString As String = lastNumber.Substring(2)
    Dim numericPortion As Integer

    If Integer.TryParse(numericPortionString, numericPortion) = False Then
        Throw New ArgumentException("Invalid numeric portion", "lastNumber")
    End If

    If numericPortion = 99999 Then
        If letterPortion = "Z"c Then
            Throw New ArgumentException("No more receipt numbers possible", "lastNumber")
        End If

        numericPortion = 0
        letterPortion = letterPortion + Chr(1)

        If letterPortion = "I"c Or letterPortion = "O"c Then
            letterPortion = letterPortion + Chr(1)
        End If
    Else
        numericPortion = numericPortion + 1
    End If

    Return String.Format("G{0}{1:00000}", letterPortion, numericPortion)
End Function

I paste it to a Visual Studio 2010 form, and I try to see what happen on Form_Load.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = GenerateReceiptNumber() '<--HERE IS THE ERROR
End Sub

But show me error in GenerateReceiptNumber().

How do I use that code to give me a receipt number in TextBox1 when I open this form?

The receipt number must be +1 next time with Form_Load.

Community
  • 1
  • 1
Pakkis
  • 1
  • 2
  • Which requirements it (receipt number) should follow? – sll Aug 12 '11 at 21:36
  • Could you please provide the full error message? – Brandon Aug 12 '11 at 21:37
  • Please read the second line of code (the function declaration), and then look at your code that calls it. The reason for the "error" (which you didn't supply, btw) should be very clear. (If it's not, study it until it is - it's really obvious.) – Ken White Aug 12 '11 at 21:40
  • Why are you creating more than one acccount? http://stackoverflow.com/users/891578/pakkis and http://stackoverflow.com/users/888649/pakkis I've [showed you](http://stackoverflow.com/questions/7016843/how-to-give-unique-number-in-each-receipt-of-payment-in-one-table-of-database-vb/7018213#7018213) the correct way to generate unique receipt-numbers. Do you have still problems getting it going? – Tim Schmelter Aug 12 '11 at 21:41
  • I did it by mistake. Sorry. Delete one of them. (Many problems. Ι apologize from users and admins) – Pakkis Aug 12 '11 at 22:10

2 Answers2

0

Glad you found my answer to the previous question!

Now, with the solution I provided mr_dunski in the other question, he required that the algorithm create a receipt number based on a previous number. So you need to start with an initial number to begin the sequence:

GenerateReceiptNumber(FirstReceiptNumber)

or

GenerateReceiptNumber("GA00000")
Adam Maras
  • 26,269
  • 6
  • 65
  • 91
  • Thanks Adam. It works.!!! Now i try to save this Number to file and next time on Form_Load check who was the last number saved there. Or if you have any good idea how to save last number and check next time to get next number.(I'm new in VS2010). – Pakkis Aug 12 '11 at 21:54
  • For that, you're going to need to look into using a database or a file of some sort. The internet has a great many resources for such things. – Adam Maras Aug 12 '11 at 22:15
0

You can write in this mode:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = GenerateReceiptNumber("GA00000")
End Sub

or

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = GenerateReceiptNumber(FirstReceiptNumber)
End Sub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131