Results 1 to 4 of 4
  1. #1
    sifuhall is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    9

    Saving a new record with automatically populated data

    Problem:



    I have a form that adds the current date to a date filled when a new record is opened. I need to be able to save the record and get the id (autonumber) of the record to use for something else.

    However, when I try to save the record in code no id has been generated yet.


    Overview:
    So, when the form is opened on a new record, the date field defaults to today.

    I then click a button to save this record so I can reference the ID. I've used

    Code:
    If Me.Dirty Then
            Me.Dirty = False
    end if
    and
    Code:
    DoCmd.RunCommand acCmdSaveRecord
    However, later (in the code of the same button) when I try to reference me.id it is always null.

    How can I force access to save this record and generate an ID for it so I can use that ID?

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    loose the Me.dirty code. It always caused trouble for me.
    But I use the same save command and it works and generates the ID. BUT be sure all your Primary key fields are filled else it wont save.

    drop a save button onto the form with wizard on, and let the wizard generate a SAVE code.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What code are you using?

    Quote Originally Posted by sifuhall View Post
    I have a form that adds the current date to a date filled when a new record is opened.
    So you have the Default Value of the date field set to DATE()?

    The autonumber value for the new record ID field will NOT be generated until you actually type something into any bound control or code places a value into a bound control. Then you can grab the ID value.
    What code are you using?


    Quote Originally Posted by sifuhall View Post
    I then click a button to save this record
    Code:
    If Me.Dirty Then
            Me.Dirty = False
    end if
    I have always use this code to save a record - never had any problems.

  4. #4
    sifuhall is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    9
    Thanks. I'll have to rethink this one.

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

Similar Threads

  1. Replies: 9
    Last Post: 10-02-2018, 03:29 AM
  2. Replies: 5
    Last Post: 08-12-2016, 11:57 AM
  3. Automatically populated fields
    By lwinford in forum Forms
    Replies: 1
    Last Post: 04-30-2013, 09:24 AM
  4. Replies: 5
    Last Post: 12-14-2011, 02:37 PM
  5. Auto Populated Field not Saving to Table
    By EstesExpress in forum Forms
    Replies: 4
    Last Post: 10-09-2009, 03:33 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