Results 1 to 7 of 7
  1. #1
    blyzz is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2015
    Posts
    3

    How to generate unique id based on another field

    I am trying to enter data into a table using a form.
    There are different fields in the form I have to generate an automatic unique id # for the records in VBA based on the Reportdate entered in an access form.


    The user would enter reportdate and the formula should generate unique id as month of reportdate+Yearreportdate+001 and increment for all the events for that month.
    So for 2 more events for same month, id would be (reportdate&Yearreportdate&002)
    For the events occurring newmonth, I need to reset the id as reportdate&Yearreportdate&001
    Then I enter the data using add record command button in the form after entering all the data and the automatically generated id field to save the data into the table.
    I am able to generate the id based on Today’s date but not sure if it is possible to do that based on the the reportdate entered in the same form.

    Any suggestion would be much appreciated.

    Thanks,
    Blyzzard

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Certainly can use the report date.

    Generating custom unique identifier is a common topic. Search forum. Here is one: https://www.accessforums.net/forms/a...ing-23329.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    you might want to consider reordering your unique id to

    YearofReportDate+MonthofReportDate+001

    reason is if you sort on it they will be in 'date' order

    e.g. your way
    02-2014-001
    02-2015-001
    03-2014-001

    my way
    2014-02-001
    2014-03-001
    2015-02-001

  4. #4
    blyzz is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2015
    Posts
    3
    I will reorder them for Year+Month+001 but I am still not sure how to refer to the report date in the VBA code.
    Right now I am using Now() to extract Month and Year

    strCurrentYear = Right(Year(Now()), 2)
    strCurrentMonth = Month(Now())

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Reference textbox on form.

    strCurrentYear = Right(Me.textboxname,2)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    blyzz is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2015
    Posts
    3
    I am new to access.
    I wrote the code in a new module and not in the form.
    I am using the sql statement in the form to insert data from form to the table.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You mean an INSERT or UPDATE sql action?

    Did you review the referenced link? That code is a custom function that is called by multiple forms to generate unique identifier. Function can be structured to have required argument(s). Arguments can be the year (and month) value. Pass value by argument when calling function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 3
    Last Post: 01-28-2014, 02:33 PM
  2. Replies: 6
    Last Post: 05-05-2013, 02:12 PM
  3. Replies: 5
    Last Post: 07-06-2012, 03:22 PM
  4. Replies: 8
    Last Post: 03-16-2012, 01:07 PM
  5. lost - linking date field based on a record with smaller Unique ID
    By stan.chernov@gmail.com in forum Queries
    Replies: 7
    Last Post: 09-16-2010, 02:22 PM

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