• Auto-incrementing invoice number (all versions)

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Auto-incrementing invoice number (all versions)

    Author
    Topic
    #387903

    (Edited by HansV on 22-May-03 10:07. Made subject more descriptive. “Excel” as subject is not very helpful.)

    I need to produce and excel template which has self incrementing invoice number? Thank you in advance for any ideas…

    Viewing 0 reply threads
    Author
    Replies
    • #678822

      Is this template to be used in a multi-user environment, i.e. is there a chance that several users request a new invoice number at more or less the same time?

      If not, you could store the most recently assigned number in a small text file. When the user requests a new number, retrieve the number from the file, increase it by one, and store the new number.

      In a multi-user environment, you’d have to store the number in a database, for a text file is not multi-user.

      • #678830

        I worked around the multi-user issue like this:

                Do
                    lCountTimes = lCountTimes + 1
                    Err.Clear
                    Open sReferencePath & sCounterFileName For Input Lock Read Write As #1
                    If Err.Number = 53 Or Err.Number = 75 Then
                        MsgBox "Counterfile not found.", , sAppName
                        Exit Sub
                    End If
                    If Err.Number = 70 Then
                        Close #1
                    Else
                        For iCount = 1 To Len(sTypeList)
                            Input #1, lCounterArray(iCount)
                        Next
                        lCounterArray(iLetter) = lCounterArray(iLetter) + 1
                        For iCount = 1 To Len(sTypeList)
                            Write #1, lCounterArray(iCount)
                        Next
                        Close #1
                        bNumberOk = True
                    End If
                Loop Until Err.Number = 0 Or lCountTimes = 100
        
        • #678835

          Thanks! I’ll also mention this in a reply to post 255643 in the VBA board about a similar question for Word.

          • #678854

            It does seem to work OK. I get no complaints about duplicate numbers.

        • #681249

          Jan Karel,

          How can you write to a file when it is opened for input?

          • #681516

            Hi Hans,

            I inadvertantly omitted three lines from the code This should’ve been it::

            Do
            lCountTimes = lCountTimes + 1
            Err.Clear
            Open sReferencePath & sCounterFileName For Input Lock Read Write As #1
            If Err.Number = 53 Or Err.Number = 75 Then
            MsgBox “Counterfile not found.”, , sAppName
            Exit Sub
            End If
            If Err.Number = 70 Then
            Close #1
            Else
            For iCount = 1 To Len(sTypeList)
            Input #1, lCounterArray(iCount)
            Next
            lCounterArray(iLetter) = lCounterArray(iLetter) + 1
            Close #1
            DoEvents
            Open sReferencePath & sCounterFileName For Output Lock Read Write As #1
            For iCount = 1 To Len(sTypeList)
            Write #1, lCounterArray(iCount)
            Next
            Close #1
            bNumberOk = True
            End If
            Loop Until Err.Number = 0 Or lCountTimes = 100

            • #681531

              Thanks, Jan Karel, I thought it might be something like that.

            • #681634

              Sorry for causing confusiog. It was a rather old piece of code, which I wrongfully edited before posting, thinking why I would close and reopen the file.

              As you can see there is a tiny chance on a conflict if a user tries to open the file whilst another user is exactly at the DoEvents command of the macro.

              Of course that could be trapped too, but I thought that chance to be too small to bother.

    Viewing 0 reply threads
    Reply To: Reply #681634 in Auto-incrementing invoice number (all versions)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel