How to create autonumber

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Apple

    #1

    How to create autonumber

    May anyone can teach me how to assign a autonumber, I want to create a
    number that is starting with year(auto change to year 2006) +
    autonumber (eg. 2005-0001, 2005-0002)
  • Rick Brandt

    #2
    Re: How to create autonumber

    Apple wrote:[color=blue]
    > May anyone can teach me how to assign a autonumber, I want to create a
    > number that is starting with year(auto change to year 2006) +
    > autonumber (eg. 2005-0001, 2005-0002)[/color]

    Have two separate fields one with DateCreated defaulting to Now() and a
    second for RecordID. Even if you don't think you need the record creation
    date down to the second that could change down the road and you will be set
    if it does. In in the mean time it is an easy way to get the year that you
    want. With two fields it is easier to calculate the next ordinal number and
    it is easy to display them on your forms and reports in the format you want
    with...

    =Format(DateCre ated, "yyyy-") & Format(RecordID , "0000")

    To assign the RecordID as each record is created use the following in the
    form's BeforeUpdate event.

    If IsNull(Me.Recor dID) = True Then
    Me.RecordID = Nz(DMax("Record ID", "TabelName" "Year(DateCreat ed) =
    Year(Date())"), 0) + 1
    End If

    The above finds the highest existing RecordID value in the table created in
    the same year as the current year and then adds 1 to it. The Nz() covers
    the very first record of each new year and the If-Then block is necessary
    because BeforeUpdate can fire more than once for a given record. This IS
    the event you want to use though because it is the only one where the record
    is saved immediately after assigning the new value.


    --
    I don't check the Email account attached
    to this message. Send instead to...
    RBrandt at Hunter dot com


    Comment

    • Apple

      #3
      Re: How to create autonumber

      "Rick Brandt" <rickbrandt2@ho tmail.com> wrote in message news:<35ke83F4m a7j1U1@individu al.net>...[color=blue]
      > Apple wrote:[color=green]
      > > May anyone can teach me how to assign a autonumber, I want to create a
      > > number that is starting with year(auto change to year 2006) +
      > > autonumber (eg. 2005-0001, 2005-0002)[/color]
      >
      > Have two separate fields one with DateCreated defaulting to Now() and a
      > second for RecordID. Even if you don't think you need the record creation
      > date down to the second that could change down the road and you will be set
      > if it does. In in the mean time it is an easy way to get the year that you
      > want. With two fields it is easier to calculate the next ordinal number and
      > it is easy to display them on your forms and reports in the format you want
      > with...
      >
      > =Format(DateCre ated, "yyyy-") & Format(RecordID , "0000")
      >
      > To assign the RecordID as each record is created use the following in the
      > form's BeforeUpdate event.
      >
      > If IsNull(Me.Recor dID) = True Then
      > Me.RecordID = Nz(DMax("Record ID", "TabelName" "Year(DateCreat ed) =
      > Year(Date())"), 0) + 1
      > End If
      >
      > The above finds the highest existing RecordID value in the table created in
      > the same year as the current year and then adds 1 to it. The Nz() covers
      > the very first record of each new year and the If-Then block is necessary
      > because BeforeUpdate can fire more than once for a given record. This IS
      > the event you want to use though because it is the only one where the record
      > is saved immediately after assigning the new value.[/color]

      Thank you Brandt. I have tried your suggestion, when I input in FORM -
      PROPERTY - FORMAT the followings : "=Format(DateCr eated,
      "yyyy-")........, it will turn to
      ("=F"\m"at)d"at "ec\re"at"ed",y yyy-)&f"\rm"at(R"ec o\rd\ID",0000)"

      Please tell me what's wrong with this?

      Comment

      • Douglas J. Steele

        #4
        Re: How to create autonumber

        "Apple" <ttt_fff2003@ya hoo.com.hk> wrote in message
        news:55044da2.0 502120338.5b4ae c74@posting.goo gle.com...[color=blue]
        > "Rick Brandt" <rickbrandt2@ho tmail.com> wrote in message
        > news:<35ke83F4m a7j1U1@individu al.net>...[color=green]
        >> Apple wrote:[color=darkred]
        >> > May anyone can teach me how to assign a autonumber, I want to create a
        >> > number that is starting with year(auto change to year 2006) +
        >> > autonumber (eg. 2005-0001, 2005-0002)[/color]
        >>
        >> Have two separate fields one with DateCreated defaulting to Now() and a
        >> second for RecordID. Even if you don't think you need the record creation
        >> date down to the second that could change down the road and you will be
        >> set
        >> if it does. In in the mean time it is an easy way to get the year that
        >> you
        >> want. With two fields it is easier to calculate the next ordinal number
        >> and
        >> it is easy to display them on your forms and reports in the format you
        >> want
        >> with...
        >>
        >> =Format(DateCre ated, "yyyy-") & Format(RecordID , "0000")
        >>
        >> To assign the RecordID as each record is created use the following in the
        >> form's BeforeUpdate event.
        >>
        >> If IsNull(Me.Recor dID) = True Then
        >> Me.RecordID = Nz(DMax("Record ID", "TabelName" "Year(DateCreat ed) =
        >> Year(Date())"), 0) + 1
        >> End If
        >>
        >> The above finds the highest existing RecordID value in the table created
        >> in
        >> the same year as the current year and then adds 1 to it. The Nz() covers
        >> the very first record of each new year and the If-Then block is necessary
        >> because BeforeUpdate can fire more than once for a given record. This IS
        >> the event you want to use though because it is the only one where the
        >> record
        >> is saved immediately after assigning the new value.[/color]
        >
        > Thank you Brandt. I have tried your suggestion, when I input in FORM -
        > PROPERTY - FORMAT the followings : "=Format(DateCr eated,
        > "yyyy-")........, it will turn to
        > ("=F"\m"at)d"at "ec\re"at"ed",y yyy-)&f"\rm"at(R"ec o\rd\ID",0000)"
        >
        > Please tell me what's wrong with this?[/color]

        Rick wasn't suggesting you put that in the Format property. You put the
        formula he gave into the text box's ControlSource property.

        --
        Doug Steele, Microsoft Access MVP

        (no e-mails, please!)





        Comment

        • Alan Webb

          #5
          Re: How to create autonumber

          Apple,
          What I do is keep a one row table with a column for each seperate sequence
          number I need. If I only need one then it's only a one row, one column
          table. I store the last sequence number used in that table then run an
          update statement to iterate that number each time I need the next number.
          This way more than one copy of the application can be runing and get the
          next sequence number as needed.

          "Apple" <ttt_fff2003@ya hoo.com.hk> wrote in message
          news:55044da2.0 502120338.5b4ae c74@posting.goo gle.com...[color=blue]
          > "Rick Brandt" <rickbrandt2@ho tmail.com> wrote in message
          > news:<35ke83F4m a7j1U1@individu al.net>...[color=green]
          >> Apple wrote:[color=darkred]
          >> > May anyone can teach me how to assign a autonumber, I want to create a
          >> > number that is starting with year(auto change to year 2006) +
          >> > autonumber (eg. 2005-0001, 2005-0002)[/color]
          >>
          >> Have two separate fields one with DateCreated defaulting to Now() and a
          >> second for RecordID. Even if you don't think you need the record creation
          >> date down to the second that could change down the road and you will be
          >> set
          >> if it does. In in the mean time it is an easy way to get the year that
          >> you
          >> want. With two fields it is easier to calculate the next ordinal number
          >> and
          >> it is easy to display them on your forms and reports in the format you
          >> want
          >> with...
          >>
          >> =Format(DateCre ated, "yyyy-") & Format(RecordID , "0000")
          >>
          >> To assign the RecordID as each record is created use the following in the
          >> form's BeforeUpdate event.
          >>
          >> If IsNull(Me.Recor dID) = True Then
          >> Me.RecordID = Nz(DMax("Record ID", "TabelName" "Year(DateCreat ed) =
          >> Year(Date())"), 0) + 1
          >> End If
          >>
          >> The above finds the highest existing RecordID value in the table created
          >> in
          >> the same year as the current year and then adds 1 to it. The Nz() covers
          >> the very first record of each new year and the If-Then block is necessary
          >> because BeforeUpdate can fire more than once for a given record. This IS
          >> the event you want to use though because it is the only one where the
          >> record
          >> is saved immediately after assigning the new value.[/color]
          >
          > Thank you Brandt. I have tried your suggestion, when I input in FORM -
          > PROPERTY - FORMAT the followings : "=Format(DateCr eated,
          > "yyyy-")........, it will turn to
          > ("=F"\m"at)d"at "ec\re"at"ed",y yyy-)&f"\rm"at(R"ec o\rd\ID",0000)"
          >
          > Please tell me what's wrong with this?[/color]


          Comment

          • Apple

            #6
            Re: How to create autonumber

            "Douglas J. Steele" <NOSPAM_djsteel e@NOSPAM_canada .com> wrote in message news:<2NWdnXZMn ZEpYZDfRVn-tA@rogers.com>. ..[color=blue]
            > "Apple" <ttt_fff2003@ya hoo.com.hk> wrote in message
            > news:55044da2.0 502120338.5b4ae c74@posting.goo gle.com...[color=green]
            > > "Rick Brandt" <rickbrandt2@ho tmail.com> wrote in message
            > > news:<35ke83F4m a7j1U1@individu al.net>...[color=darkred]
            > >> Apple wrote:
            > >> > May anyone can teach me how to assign a autonumber, I want to create a
            > >> > number that is starting with year(auto change to year 2006) +
            > >> > autonumber (eg. 2005-0001, 2005-0002)
            > >>
            > >> Have two separate fields one with DateCreated defaulting to Now() and a
            > >> second for RecordID. Even if you don't think you need the record creation
            > >> date down to the second that could change down the road and you will be
            > >> set
            > >> if it does. In in the mean time it is an easy way to get the year that
            > >> you
            > >> want. With two fields it is easier to calculate the next ordinal number
            > >> and
            > >> it is easy to display them on your forms and reports in the format you
            > >> want
            > >> with...
            > >>
            > >> =Format(DateCre ated, "yyyy-") & Format(RecordID , "0000")
            > >>
            > >> To assign the RecordID as each record is created use the following in the
            > >> form's BeforeUpdate event.
            > >>
            > >> If IsNull(Me.Recor dID) = True Then
            > >> Me.RecordID = Nz(DMax("Record ID", "TabelName" "Year(DateCreat ed) =
            > >> Year(Date())"), 0) + 1
            > >> End If
            > >>
            > >> The above finds the highest existing RecordID value in the table created
            > >> in
            > >> the same year as the current year and then adds 1 to it. The Nz() covers
            > >> the very first record of each new year and the If-Then block is necessary
            > >> because BeforeUpdate can fire more than once for a given record. This IS
            > >> the event you want to use though because it is the only one where the
            > >> record
            > >> is saved immediately after assigning the new value.[/color]
            > >
            > > Thank you Brandt. I have tried your suggestion, when I input in FORM -
            > > PROPERTY - FORMAT the followings : "=Format(DateCr eated,
            > > "yyyy-")........, it will turn to
            > > ("=F"\m"at)d"at "ec\re"at"ed",y yyy-)&f"\rm"at(R"ec o\rd\ID",0000)"
            > >
            > > Please tell me what's wrong with this?[/color]
            >
            > Rick wasn't suggesting you put that in the Format property. You put the
            > formula he gave into the text box's ControlSource property.[/color]


            Thank you Doug, I had tried to put the formula into FORM-TEXT
            BOX-CONTROL SOURCE PROPERTY or FORM-UNBOUNDED TEST BOX-CONTROL SOURCE
            PROPERTY, but it show error(#xxxx?); when I tried to put into a query
            as an expression, when open up the query or form, it will ask to enter
            an parameter (1) date; (2) number, if I entre (1) 2005/01/01; (2)
            0001, it will show 2005-0001 in all record that I have input data
            before.

            The following is in my form's BeforeUpdate event :

            Private Sub RecordID_Before Update(Cancel As Integer)
            If IsNull(Me.Recor dID) = True Then
            Me.RecordID = Nz(DMax("Record ID", "FORM
            NAME""Year(Date Created)=Year(D ate())"), 0) + 1
            End If


            End Sub


            Please comment!!

            Comment

            Working...