Results 1 to 5 of 5
  1. #1
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94

    Action on record before PK is created (record saved)

    Hi All:

    I have a situation where I have a "Click to add" function on a subform that lists records in another table. As the last record in the subform, it's listed as "New" instead of "Edit".

    When I click on "New", I have an open form command with a where "[ID]=" & Nz([ID],0) statement.

    This works well. The problem is that when the subform opens, and data is entered, I have a command button to start a procedure based on the PK ([ID]). Since no ID exists yet (assigned on save), the function requiring the PK reports no valid PK.

    I have attempted to insert an

    Code:
    if me.dirty then me.dirty=false
    but then it replaces the data in the form with #deleted and give me a write conflict error when I attempt to save the record again (close form).

    Question:



    How do I save the record to assign the PK in [ID] and then pass that value to the functions that require it?

    Thanks...

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    You could define the PK as soon as the record is created in the sub-form, using the BeforeInsert event. This event fires as soon as you type the first character in a new record.

    John

  3. #3
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94
    The PK is an auto-increment field. It only get's written when the record is saved the first time. If I open the subform and then save the record (and close the subform), then go back into it, everything works (because there is a value in the PK). What I want to do is avoid the closing and reopening.

  4. #4
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    What do you mean by "Auto-increment"? There is no such data type in MS Access. If you are using an autonumber field, the autonumber is assigned as soon as a character is typed in a new record. If you are not using autonumber data type, how is the number generated?

    John

  5. #5
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94
    It's a MySQL back-end and it's AI. The AI PK is assigned as soon as the record is saved.

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

Similar Threads

  1. Replies: 8
    Last Post: 07-18-2012, 11:11 AM
  2. Replies: 2
    Last Post: 03-28-2012, 02:41 PM
  3. Autofilling when New Record Created from Form
    By SpdRacerX in forum Forms
    Replies: 6
    Last Post: 01-24-2012, 10:24 AM
  4. Replies: 1
    Last Post: 04-19-2011, 04:46 PM
  5. Who created new record
    By ducecoop in forum Access
    Replies: 10
    Last Post: 10-28-2010, 02:50 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