Results 1 to 7 of 7
  1. #1
    Fish218 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    68

    Reset autonumber in a concatenate field

    I would like to build a concatenate field using Year, ID Number, and a 3-digit autonumber. Year is a combo box in a form (YYYY), NumID is the two or three digit ID Number for a person, and then the three digit autonumber. I would also like the three digit autonumber to follow in sequence based on Year and NumID. For example, when starting a new year I would like the autonumber to reset to 000. When entering data for multiple people, I would like the autonumber to advanced based on the last autonumber for that person, not the next autonumber in the list. Any suggestions?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I don't think autonumber is what you need or want. Autonumbers are guaranteed to be unique, but not necessarily in sequence.
    You may wish to build a function to create the numbers you want

    YYYY from current year a number that you control and adjust by NewValue = Dmax(value) +1

    However, you may want to tell us why exactly you want/need/have selected this scheme. What is the purpose of this that can't/shouldn't/couldn't be achieved by some other means. Your rationale may be perfect and justified, but it isn't necessarily an obvious choice.


    Autonumbers are usually used as primary keys for tables, and handled exclusively by Access (or other DBMS) for system usage. The autonumbers are not intended for human consumption/use iiiin most (if not all) situations.

  3. #3
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    First, the word year is a reserved word in Access, so it should not be used as a field name (I used myYear as the field name in the example below). With that said, you will need some Visual Basic for Application (VBA) code to do what you want. The code will look something like this (air code, not tested)


    Code:
    IF DCount("*","yourtablename", "myYear=" & YourYearCombo & " AND NumID=" & controlname)=0 THEN
      myincrementcontol=0
    ELSE
      myincrementcontrol=1+DMax("myincrement","yourtablename", "myYear=" & YourYearCombo & " AND numID=" & controlname)
    END IF
    I believe the code needs to be in the before insert event of the form, but i may be wrong. You will probably want some code prior to the above to check to make sure that a year was selected from the combo box and that the NumID was entered and is valid.

  4. #4
    Fish218 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    68
    What is the "*" after DCount( ?

  5. #5
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    It is a wild card to represent any field since you are just counting records. I believe that you could substitute one of the fieldnames from the table if you want. The general form of all the domain aggregate functions is : functionname("fieldname", "query or table name", "criteria")

  6. #6
    Fish218 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    68
    I figured this one out by using a query to lookup the number of fish caught (varFishCount), by year and collector. Thanks for pointing me in the right direction!

    Private Sub Length_AfterUpdate()
    Dim varYear As Integer
    Dim varCollector As Integer
    Dim varFishCount
    varYear = [Forms]![frmCommercial2]![Form1]![Text161]
    varCollector = [Forms]![frmCommercial2]![Form1]![CollectorID]
    varFishCount = DLookup("[CountofIndividualFishID]", "qry2012", "[CollectionYear]=" & varYear & " And [CollectorID]=" & varCollector)
    If (IsNull(varFishCount)) Then
    [Text51] = 0
    [Text53] = 1
    ElseIf Not (IsNull(varFishCount)) Then
    [Text51] = varFishCount
    [Text53] = varFishCount + 1
    End If
    [FishNum] = [Forms]![frmCommercial2]![Form1]![Text161] & [Forms]![frmCommercial2]![Form1]![CollectorID] & (Format([Text53], "0000"))
    End Sub

  7. #7
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Glad you worked out a solution!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Customizing the Autonumber field
    By wasim_sono in forum Access
    Replies: 3
    Last Post: 10-24-2014, 03:00 PM
  2. Replies: 6
    Last Post: 04-07-2011, 12:33 PM
  3. Query to Reset AutoNumber to 1 after full delete
    By saigovind in forum Queries
    Replies: 1
    Last Post: 02-04-2011, 03:43 AM
  4. Autofill field based on autonumber field
    By frevilla in forum Forms
    Replies: 1
    Last Post: 09-11-2009, 02:50 AM
  5. Concatenate two fields (text & number) for key field
    By Larry Elfenbein in forum Forms
    Replies: 2
    Last Post: 11-10-2005, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums