self generating number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • didacticone
    Contributor
    • Oct 2008
    • 266

    self generating number

    i am using the following code to generate a number in a text box on a form when data is entered into the form:
    Code:
    Private Sub Form_BeforeInsert(Cancel As Integer)
      Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
    End Sub
    what i would like to know is, is there any way that when the new year comes the number will restart at 0001. i do have a year field in my table as it is the first part of our unique ticket number system.. the number would display as
    "09-0001" with "09-" being the default value for the year field that i will change every year. hope thats enough info to make sense... any help is greatly appreciated. thanks!
    Last edited by NeoPa; Feb 8 '09, 11:29 AM. Reason: Please use the [CODE] tags provided
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    You can use the DMax function's criteria to look for the maximum ticket number for the current year.

    Code:
    DMax("[ticketnum]", "master", "YearFieldName = '" _
    & Format(Date,"yy") & "-'")

    Comment

    • didacticone
      Contributor
      • Oct 2008
      • 266

      #3
      ok very newb question here...but where would i place that code... and do i have to eliminate the default value in my year field on the table?

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by didacticone
        i am using the following code to generate a number in a text box on a form when data is entered into the form:

        Private Sub Form_BeforeInse rt(Cancel As Integer)
        Me![TICKETNUM] = Format(Nz(DMax( "[ticketnum]", "master"), 0) + 1, "0000")
        End Sub


        what i would like to know is, is there any way that when the new year comes the number will restart at 0001. i do have a year field in my table as it is the first part of our unique ticket number system.. the number would display as
        "09-0001" with "09-" being the default value for the year field that i will change every year. hope thats enough info to make sense... any help is greatly appreciated. thanks!
        I just threw this together off the top-of-my-head in work, but it may actually, in fact, work:
        1. Create a Table named tblYear.
        2. This Table will consist of 1 Field only, named [Year] - {LONG}.
        3. Make this Table Hidden, so that it is not Visible to the average User.
        4. Execute the following code wherever appropriate.
          Code:
          Dim lngStoredYear As Long
          
          lngStoredYear = DLookup("[Year]", "tblYear")
          
          If lngStoredYear = Year(Date) Then
            'Year is the same, Increment by 1 and Format
            Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
          Else    'different Year, Reset TICKETNUM to 0001, Update [Year] in tblYear by 1 Year
            Me![TICKETNUM] = "0001"
            CurrentDb.Execute "Update tblYear Set tblYear.[Year] = " & lngStoredYear + 1, dbFailOnError
          End If
        5. Any questions, feel free to ask.

        Comment

        • ChipR
          Recognized Expert Top Contributor
          • Jul 2008
          • 1289

          #5
          Assuming the code you have works, just change
          Code:
          Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
          to
          Code:
          Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master", _
               "YearFieldName = '" & Format(Date,"yy") & "-'"), 0) + 1, "0000")
          Last edited by NeoPa; Feb 8 '09, 11:32 AM. Reason: Please use the [CODE] tags provided

          Comment

          • didacticone
            Contributor
            • Oct 2008
            • 266

            #6
            that code gives me a syntax error...

            Comment

            • ChipR
              Recognized Expert Top Contributor
              • Jul 2008
              • 1289

              #7
              Which code and what error?

              Comment

              • didacticone
                Contributor
                • Oct 2008
                • 266

                #8
                adezii... i tried your way and placed that code in the 'beforeinsert' event of the form and it returns run time error '94': invalid use of null. and it highlights the line:

                lngStoredYear = DLookup("[Year]", "tblYear")

                any ideas? and thanks to each of you for your help

                Comment

                • didacticone
                  Contributor
                  • Oct 2008
                  • 266

                  #9
                  chipr... i used the code you gave me:
                  Code:
                  Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master", 
                  "YearFieldName = '" & Format(Date,"yy") & "-'"), 0) + 1, "0000")
                  after i paste it in vb it is all red.. and when i change the year field name to my year fields name it returns:

                  compile error:
                  expected:line number or label or statement or end of statement

                  thanks!
                  Last edited by NeoPa; Feb 8 '09, 11:34 AM. Reason: Please use the [CODE] tags provided

                  Comment

                  • ChipR
                    Recognized Expert Top Contributor
                    • Jul 2008
                    • 1289

                    #10
                    Is it really two lines? Because if it is, you need to continue it with _ &
                    along with putting in the correct name of your Year field.

                    Comment

                    • didacticone
                      Contributor
                      • Oct 2008
                      • 266

                      #11
                      ok it worked but, when i changed the year to "10-" in the table as my default value... the form doesnt reset to 1

                      Comment

                      • ChipR
                        Recognized Expert Top Contributor
                        • Jul 2008
                        • 1289

                        #12
                        Why would it reset to 1? It's looking up the highest ticket number for the current year in the table and adding 1 to it.

                        Comment

                        • didacticone
                          Contributor
                          • Oct 2008
                          • 266

                          #13
                          so its actually not using the value i input in the year field in my table at all then?

                          Comment

                          • ChipR
                            Recognized Expert Top Contributor
                            • Jul 2008
                            • 1289

                            #14
                            No, it's not using your default value for anything here.

                            Comment

                            • didacticone
                              Contributor
                              • Oct 2008
                              • 266

                              #15
                              oh ok thats where i was confused... so now when 2010 begins the number will restart at 0001? that is awesome.. thanks a ton for your help

                              Comment

                              Working...