Results 1 to 9 of 9
  1. #1
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136

    Add record to Subform using Add Record button

    Hi,


    I have an Add Record button on my form that I want to use to add records to a subform. I am using the following code:

    Private Sub cmd_Add_Record_Click()
    Forms![frm_Label_Tracking]![sfrm_Label_Tracking].SetFocus
    DoCmd.GoToRecord , , acNewRec
    End Sub

    It's giving me an error telling me it can't find the subform field 'sfrm_Label_Tracking'. What am I doing wrong? Or this the wrong way to go about it?

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    In a proper form/subform setup you don't need a button at all. Just start typing into a new row in the datasheet subform.

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Maybe try

    Me!sfrm_Label_Tracking.SetFocus

  4. #4
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136
    Quote Originally Posted by davegri View Post
    In a proper form/subform setup you don't need a button at all. Just start typing into a new row in the datasheet subform.
    My subform isn't a data sheet. It's a customized form view. At the form level I have a combo box with a list of product names. When you select a product it brings up all the records with that product name in the subform. Right now I have no way to add another record to the subform.

  5. #5
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136
    Quote Originally Posted by kd2017 View Post
    Maybe try

    Me!sfrm_Label_Tracking.SetFocus
    That didn't work. :-(

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by matey56 View Post
    Hi,
    I have an Add Record button on my form that I want to use to add records to a subform. I am using the following code:

    Private Sub cmd_Add_Record_Click()
    Forms![frm_Label_Tracking]![sfrm_Label_Tracking].SetFocus
    DoCmd.GoToRecord , , acNewRec
    End Sub

    It's giving me an error telling me it can't find the subform field 'sfrm_Label_Tracking'. What am I doing wrong? Or this the wrong way to go about it?
    You're code actually works fine in my test. Double check that you haven't misspelled something.

    To davegri's point, you can click the little arrow with a star on the bottom left of the subform to jump to a new record.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    Perhaps Me.[sfrm_Label_Tracking].Form.SetFocus ?

    Found this with a quick Google, have a read. http://datagnostics.com/dtips/formsubformfocus.html
    Last edited by Welshgasman; 01-26-2021 at 03:14 PM. Reason: Added Google link
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    If you really want a button, how about moving it from the main form to the subform?

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    The problem could be because you cannot Set Focus on a Form if it has one or more Controls that can have data Added/Edited. To move to a Subform , via code, you have to Set Focus to the Subform Control on the Main Form...which may or may not have the same name as the name of the Form the Subform is based on.

    The Form your Subform is based on apparently is sfrm_Label_Tracking...but is that the name of the Subform Control it resides in?

    Linq
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 4
    Last Post: 08-10-2018, 12:00 AM
  2. Replies: 13
    Last Post: 02-05-2018, 12:58 PM
  3. Replies: 1
    Last Post: 09-16-2014, 02:26 PM
  4. Replies: 6
    Last Post: 05-18-2013, 12:48 AM
  5. Replies: 5
    Last Post: 04-06-2012, 10:59 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