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

    ACCESS 2010 Update primary key in a form through a macro

    I hope I am posting this in the correct location, but here goes. This access issue has been troubling me for a while and I have yet to find a viable solution anywhere on the net.


    I have an Access DB that is split in two (front end and back end). The primary key for my table, called Notice, is set to number, however in the form I have this function: DMax("[NoticeID]","[tblNotice]")+1
    This essentially allows me to use autonumber without actually using the autonumber data type (which I would rather avoid using). So anytime a user opens the form, the next available NoticeID is assigned.
    The problem I am having is when two people have the front end DB open, and navigate to a blank entry in the form to fill they are both assigned the same NoticeID. When one user eventually saves before the other, it writes that notice to the table. However when the second user tries to save the record, the form spits out an error that you cant have duplicate primary keys.
    I have tried to use a requery macro in the BeforeUpdate event for the form and the NoticeID field however I haven't had any luck. Any help on this would be appreciated.
    Thanks,
    Zack

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    I handle this by immediately committing record with the new ID to table. Not impossible for multiple users to generate the same ID but greatly reduces the odds. This raises question of do you want to allow users to abort the entry and how do you want to handle?
    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
    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
    Because I usually give users the option to dump a New, unsaved Record, and usually don't allow Records to be saved until all data has been Validated, which cannot be easily done, after the New Record is saved, I normally do this by doing the assignment in the Form_BeforeUpdate event. Because this occurs in the last nanosecond before the Record is saved, I've never had a duplication problem, in over a decade of developing Access apps.

    I've only used June7's method once, I believe, because the users simply had to know the number (for another, non-database application) while the New Record was being entered, which happens if you assign the number in the Form_Current event and immediately save the Record.

    Linq ;0)>

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    Yes, that's my situation as well. Need to show the generated ID to users. I allow for aborting the entry and the saved (but unused) number is retrieved and used in the next record entry. Going on 4+ years and I think we got a duplication error once.
    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
    geekatron42 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2013
    Posts
    2
    Sorry for the delay in response. I would need the primary key that is generated since it is used outside of the database.

    @June7: The only problem I see with committing the key to the table right away would be that if the user aborts I want the key to still be available.

    @Missinglinq: I tried a couple of things with the Before_Update event; mainly a repaint macro however it didn't work.

    I am not too familiar with macros or VBA unfortunately, but I am working on it. Thanks again for the quick reply's

    Zack

  6. #6
    alcapps is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Jan 2012
    Posts
    292
    I like your Idea about the autonumber..

    I have done this before as well. I used the Form_BeforeInsert to handle the the timing of the ID beening saved.
    I would call the number again in the before insert event and save the id at that time. DMax("[NoticeID]","[tblNotice]")+1

    I've also.. had a timer function of the form.. to update the Id field every 10 seconds.. in a multiuser environment. You can turn up or down the checking on the form by using a default table. Load the value in the form on open or load.. say 10 seconds..

    so as the user is working the form will write the number in the id field.. The trick is in this is to have the form timer save where you were if the number has to change. After the number changes it can set the focus back. (This is not easy sometimes). If you don't need to change the number every thing happens unnoticed.

    just a thought if you wanted to try this.

    Form_BeforeInsert ..
    and Form Timmer event

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    In my setup, if the user aborts, the key is still available. What would you want to do with it? In my db the aborted key is saved without data and then is retrieved for use next time a sample is logged in.
    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: 8
    Last Post: 08-02-2012, 08:48 AM
  2. Replies: 1
    Last Post: 06-27-2012, 09:16 AM
  3. Question access 2010 macro & open form
    By Grek in forum Access
    Replies: 3
    Last Post: 10-30-2011, 01:58 PM
  4. Access 2010 Macro
    By RayMilhon in forum Access
    Replies: 3
    Last Post: 09-28-2011, 11:37 AM
  5. Replies: 0
    Last Post: 07-13-2010, 07:45 AM

Tags for this Thread

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