Results 1 to 6 of 6
  1. #1
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57

    Generate Unique table ID

    Hi



    I have a table that has invoiceID as the primary key but it is not auto number, it is string. what I want to do is in the form when it is loaded, I want to following this format:

    "Py-000" then add a number and keeps incrementing.

    Example: "Py-0001","Py-0002","Py-0003"...

    I know that I need to save the counter in the table but how to do that when mixing string and number?

  2. #2
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    IMO you should have an autonumber field for unique record id and not use real data as your pk. Then 1 field for prefix because that may get augmented at some point. Then a field for the incremental number. In forms, queries or reports you work with the id when necessary, and concatenate the prefix and number where required (in form/report controls).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    correct answer but if sequence # has to be steady ( like last record deleted , new record # has gap) opention is look up the max(#) remove the prefix add 1 and ok na

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Generating a custom unique identifier is a common topic. Review https://www.accessforums.net/showthread.php?t=23329

    If it will always be "PY", why bother saving this in field? It can be added in expression with concatenation or textbox formatting when needed.
    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.

  5. #5
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    why bother saving this in field
    Because you can never say never? What if the boss says, "we are starting a new prefix - add it to the database". Now your field will contain a mix of prefixes along with the numbers, and the numbers may have to start over again from 1. In that case, you have to extract numbers from strings to increment based on string groups when you could group by prefix and just increment the max for a group instead of separating, incrementing and piecing back together.

    I saw nothing in the op that said it will always be PY, but if that is the case, you would be right.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I did say "if". And example I referenced does show building a compound value with sequence starting over each year. Just wanted to give OP something to think about. Excluding text would simplify. I could have certainly simplified my code with restructure of data. I inherited that db half built and it was already set up with this generated ID as primary key.
    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: 2
    Last Post: 02-21-2018, 08:49 AM
  2. Replies: 8
    Last Post: 08-21-2017, 11:13 AM
  3. How to generate unique id based on another field
    By blyzz in forum Programming
    Replies: 6
    Last Post: 04-09-2015, 04:13 PM
  4. Replies: 4
    Last Post: 10-17-2014, 01:34 PM
  5. Replies: 8
    Last Post: 03-16-2012, 01:07 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