Results 1 to 2 of 2
  1. #1
    WG_Clark is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2013
    Posts
    1

    Continuous Count

    Hi! I am a novice Access user.....creating a database (MSAccess2007) for a property management group



    I have created a table "ResidentID"

    Primary key is ResidentID - rather than autonumber, I chose text format (I want to create my own sequential identification)

    I am looking to make the text format for ResidentID the year and then sequential number. ex. "2013-0001". next record would be "2013-0002"
    Not sure where or how to tell access to do this (does this fall under validation rule?)

    Also, I will be creating a form for this table. I would like the Property manager to be able to enter new residents and have the new record automatically populate the next sequential "ResidentID" number.

    Any help would be most greatly appreciated!

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.NewRecord Then
       If RecordsetClone.RecordCount = 0 Then
         Me. ResidentID = Format(Date, "YYYY") & "-0001"
      End If
    If RecordsetClone.RecordCount <> 0 Then
      If Left(DMax("ResidentID ", "YourActualTableName"), 4) = Format(Date, "YYYY") Then
       Me. ResidentID = Format(Date, "YYYY") & "-" & Format(DMax("val(Right([ResidentID],4))", "YourActualTableName", "Left([ResidentID],4) = '" & Format(Date, "YYYY") & "'") + 1, "0000")
      Else
       Me. ResidentID = Format(Date, "YYYY") & "-0001"
      End If
      End If
    End If
    End Sub


    Assuming that ResidentID is the actual name you're using, you only have to replace YourActualTableName with...that's right...Your Actual Table Name!

    This kind of hack simply has to be in the Form_BeforeUpdate event, if this is going to be used in a multi-user environment; otherwise two users could each start a Record and be assigned the same ID number. Placing it in the Form_BeforeUpdate event insures that the assignment takes place in the last nanosecond before the Record is saved. I've had this kind of code running, in this event, in apps for well over a decade and I've never had a duplicate number assigned. The only drawback, with this, is the same thing; the number is assigned as the Record is saved, usually when moving to another Record, closing the Form or closing Access, itself, meaning that you won't see it, on a Single View Form, until you return to the Record.

    If this is going to be a stand-alone app, i.e. one user using it on a single computer, you can move the code, in toto, to the Form_Current event. If you do that you just have to make one modification:

    Directly under the Sub Header, before any other code, enter the line

    If Me.NewRecord Then

    Then immediately before the last line of the Sub, i.e.

    End Sub

    enter

    End If

    So the last two lines should be

    End If
    End Sub

    Linq ;0)>

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

Similar Threads

  1. COUNT Group Count Records COUNT FUNCTION
    By PMCOFFEY in forum Access
    Replies: 9
    Last Post: 11-09-2012, 09:40 PM
  2. Auto Count Month in Continuous Form
    By Mpike926 in forum Forms
    Replies: 10
    Last Post: 05-18-2012, 11:18 AM
  3. How to bin a continuous data set in SQL
    By EvanRosenlieb in forum Access
    Replies: 12
    Last Post: 04-13-2012, 02:27 PM
  4. Non-continuous form
    By alyon in forum Access
    Replies: 6
    Last Post: 07-19-2011, 12:25 PM
  5. Continuous Form
    By duckie10 in forum Access
    Replies: 13
    Last Post: 06-09-2009, 11:15 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