Results 1 to 12 of 12
  1. #1
    KathyL is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    94

    On Add record, how do I force a Save first?

    I've used the button wizard to do a save record. (I realize Access saves record data when the record is advanced to any other record.) The wizard generates this VB code: "DoCmd.RunCommand acCmdSaveRecord".

    I have an issue where the User is adding a record on the form. They want to directly print when they've finished keying in data. It won't print on the first click, it does nothing, and so they click twice.

    I believe the problem is because the record isn't yet in the table. I can view the table in another tab and that's true, the record isn't there yet.



    So I've tried putting the standard 'Save record button' on the form, but when the form is in "add mode", the save record gives me an error of: "You entered an expression that has an invalid reference to the property".

    How do I force Access to save the record on an Add function without closing the form or advancing to another record?

    Thanks to anyone who can help!

  2. #2
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    This could be caused by a timing error. Have you put the DoCmd immediately before a statement trying to access the new record?

    There's nothing wrong with your method of forcing a save.

  3. #3
    KathyL is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    94
    Quote Originally Posted by Rod View Post
    This could be caused by a timing error. Have you put the DoCmd immediately before a statement trying to access the new record?

    There's nothing wrong with your method of forcing a save.
    It's not an issue of a timing error. I placed a standard button, created by the MS wizard, of saving a record, right on my form. So then, I just go into add mode, by clicking the standard MS add mode navigation button. Once a new blank record is on the form, I fill in some data, and then I click on my 'save record' button.. and get that error.

    If I'm on an old record, and do some changes to data, and click the 'save record' button, it works just fine.

    It just doesn't work when creating a new record.

    addendum: I just made a new form... very basic... and put the same functions on it. When I add a record, and then do the save record, I'm getting a message "The RunCommand action was cancelled". But it appears the record was saved. I don't understand the error.

  4. #4
    Stanggirlie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2009
    Location
    Iowa
    Posts
    25
    I got this error when I first started using Access and if I remember right, it's because you can't save what is not there - hence the error. With Access, when you add a new record, it doesn't "register" the record until you essentially leave that record somehow. I may be a little fuzzy on this because it was so long ago, but I sort of remember that.

    So, to solve the issue for the staff I was working with, I altered the "save" button. I created a macro that when you clicked the "save" button, it went to a new record, then back to the latest record, which is the one you just entered (I used the GotoRecord function). It is instantaneous as it flashes the screen for a mili-second.

    This accomplishes two things. One, it saved your record. Two, you are on the record you just entered so you can do whatever you need to from there. I don't know any VB coding so this worked for me. Macros are my best friend - they allow you to accomplish a lot and are pretty easy to work with.

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    Hi

    Instead of using:
    Code:
     
    DoCmd.RunCommand acCmdSaveRecord
    You may like to try using:
    Code:
     
    If Me.Dirty = True Then
        Me.Dirty = False
    End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    KathyL is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    94
    Quote Originally Posted by Stanggirlie View Post
    I got this error when I first started using Access and if I remember right, it's because you can't save what is not there - hence the error. With Access, when you add a new record, it doesn't "register" the record until you essentially leave that record somehow. I may be a little fuzzy on this because it was so long ago, but I sort of remember that.

    So, to solve the issue for the staff I was working with, I altered the "save" button. I created a macro that when you clicked the "save" button, it went to a new record, then back to the latest record, which is the one you just entered (I used the GotoRecord function). It is instantaneous as it flashes the screen for a mili-second.

    This accomplishes two things. One, it saved your record. Two, you are on the record you just entered so you can do whatever you need to from there. I don't know any VB coding so this worked for me. Macros are my best friend - they allow you to accomplish a lot and are pretty easy to work with.
    I had tried this, got messages I couldn't go to another record. I'm going to keep trying things. My large form has a control tab with 3 subforms so I may just be pushing it too much.
    I've tried the me.dirty thing too... I think I've tried everything I could find on the net.

    Thank you both for your feedback!

  7. #7
    Stanggirlie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2009
    Location
    Iowa
    Posts
    25
    You can also try going "back" a record - you don't have to go to a new record. I've done both and it works the same. I'm sorry it didn't work.

  8. #8
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    If the form is dirty using

    Me.Dirty = False

    will fire the form's Before Update event which then the data can be validated and the update stopped if it doesn't pass. But if all is well with that and it gets to the end of the Before Update event and then throws an error, I belive there may be some structural problems. Any chance of getting a copy of the database with some bogus test data in it so we can take a look?

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    This may be very caveman-esque (because I don't have access to a printer at the moment) but you could try something like:

    DoCmd.GoToRecord acDataForm, "<formname>", acNewRec
    DoCmd.GoToRecord acDataForm, "<formname>", acLastRec

    This would force a save by going to a new record then immediately back up to the record you just entered assuming you're always working off the end of a dataset, if not you may be able to get to it with acPrevious.

  10. #10
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    I agree with Bob that it sounds as though there's something amiss with your structure.

    But ...

    Have you tried dropping the DoCmd and simply coding

    Code:
    RunCommand acCmdSaveRecord
    or more explicit

    Code:
    Application.RunCommand acCmdSaveRecord
    I can't see how it would make any difference but you never know.

  11. #11
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Also try

    Code:
     
    DoCmd.DoMenuItem acFormBar, acFile, acSaveRecord
    Or even
    Code:
    SendKeys "+{ENTER}", True
    Last edited by Rod; 07-18-2011 at 02:57 AM.

  12. #12
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Sendkeys isn't going to work with Access 2007 and on Win7.

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

Similar Threads

  1. do NOT save record on close (x button)
    By benjammin in forum Forms
    Replies: 12
    Last Post: 03-15-2013, 02:54 AM
  2. Save record to 2 tables on one click
    By chin1383 in forum Forms
    Replies: 1
    Last Post: 04-01-2011, 12:49 PM
  3. Duplicate Record Cannot Save
    By magicscreen in forum Programming
    Replies: 2
    Last Post: 09-15-2010, 08:15 AM
  4. save last record in table through a form
    By ajetrumpet in forum Forms
    Replies: 3
    Last Post: 09-09-2010, 08:53 AM
  5. Force form not to save updated fields
    By Evgeny in forum Programming
    Replies: 2
    Last Post: 04-30-2010, 10:44 PM

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