Results 1 to 9 of 9
  1. #1
    user9 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2013
    Location
    US
    Posts
    43

    Add Record to Subform from Main Form - Error "you can't go to the specified record"

    I have three forms. All are connected to tblComp.


    1. frmMain
    2. frmSelect (subform embedded in main form) - read-only as Datasheet
    3. frmRecords (popup form) - editable

    I'm looking to add a new record from clicking a button on frmMain, which creates a new record in frmRecords with only the primary key (RV_ID) populated. I need the new record to be a new incremental value for RV_ID by finding the highest value of RV_ID in tblComp and adding 1 (presumably using DMax).

    I've tried several different methods based on other posts with no luck (getting the error 2105 - "You can't go to the specified record". Below is my current code, any guidance/suggestions would be greatly appreciated.

    Code:
        DoCmd.OpenForm "frmRecords"
        DoCmd.GoToRecord , , acNewRec
        [RV_ID] = Nz(DMax("RV_ID", "tblComp") + 1)

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Edit: the acFormAdd makes the most sense
    Last edited by ItsMe; 11-25-2013 at 02:53 PM. Reason: Just Realized

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    The code is behind main form so I think it is trying to move to new record on main form and set value of [RV_ID] field on main form.

    Probably easier to open the form in Add mode.

    DoCmd.OpenForm "frmRecords", , , , acFormAdd

    Then code in the popup form Current event:

    If IsNull(Me!RV_ID) Then 'or use If Me.NewRecord
    Me!RV_ID = Nz(DMax("RV_ID", "tblComp") + 1)
    DoCmd.RunCommand acCmdSaveRecord
    End If

    Is this a multi-user database?

    EDIT: I see ItsMe got a post in just before me. As you can see, more than one way ...
    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.

  4. #4
    user9 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2013
    Location
    US
    Posts
    43

    Re: Add Record to Subform from Main Form Error "you can't go to the specified record"

    Quote Originally Posted by June7 View Post
    The code is behind main form so I think it is trying to move to new record on main form and set value of [RV_ID] field on main form.

    Probably easier to open the form in Add mode.

    DoCmd.OpenForm "frmRecords", , , , acFormAdd

    Then code in the popup form Current event:

    If IsNull(Me!RV_ID) Then 'or use If Me.NewRecord
    Me!RV_ID = Nz(DMax("RV_ID", "tblComp") + 1)
    DoCmd.RunCommand acCmdSaveRecord
    End If

    Is this a multi-user database?

    EDIT: I see ItsMe got a post in just before me. As you can see, more than one way ...
    This will initially be a 2-user database, with potential for more down the line. Your code worked, save for the DoCmd on which I got the run-time error 2501 "the RunCommand action was cancelled". I commented this line out and see the record saved on my Save Record click so I should be all set there.

    Thanks to both of you for the very quick and knowledgable replies!

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    I added that line because if this is a multi-user database, immediately committing the new ID to table will prevent another user from calculating the same ID.

    That is what I do but my code is a bit different. I was hoping this one would work.
    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.

  6. #6
    user9 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2013
    Location
    US
    Posts
    43

    Add Record to Subform from Main Form - Error "you can't go to the specified record"

    Quote Originally Posted by June7 View Post
    I added that line because if this is a multi-user database, immediately committing the new ID to table will prevent another user from calculating the same ID.

    That is what I do but my code is a bit different. I was hoping this one would work.
    In that event, what causes the error I'm getting? I appreciate the extra code to make it more flexible, just not sure why I get that error sometimes on the DoCmd for save.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Which Docmd is causing the error? If it is the OpenForm....

    Add a line to save the record before opening the new form. So, before your DoCmd.OpenForm "frmRecords", , , , acFormAdd

    add
    If Me.dirty = true then me.dirty = false

    If it is the other Docmd, maybe you are placing a duplicate in a field that does not allow duplicate keys.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    ItsMe, not sure how that saves record with new RV_ID.

    I just re-read the OP which states that all 3 forms are connected to tblComp. Why would frmMain and frmSelect have same dataset?

    In my procedure, I use sql action to save record with new ID then open form filtered to that new record.
    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.

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by June7 View Post
    ItsMe, not sure how that saves record with new RV_ID.
    It does not. I was simply suggesting to add a line of code that would help to discern where the error occurs. Perhaps the current form is not able to lose focus. If this is the case, it will debug on the line I provided.

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

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2013, 12:33 PM
  2. Replies: 2
    Last Post: 11-12-2013, 07:06 PM
  3. Replies: 10
    Last Post: 03-15-2013, 05:46 AM
  4. "You can't save this record at this time" error
    By kelann in forum Programming
    Replies: 4
    Last Post: 10-29-2012, 02:21 PM
  5. Replies: 1
    Last Post: 09-11-2010, 08:21 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