Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    dhicks is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    36

    SetFocus to a Subform field


    Is it possible to use the SetFocus method to move from a Main form command button On Click event to a specific field in a new record on a Subform? When I click on the command button, a new record is created in the subform and populates the first 2 fields in the record. I want the cursor to end up in the 3rd field for manual entry of data. I've tried several different ways, but nothing seems to work. Any ideas? Thanks.

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Have that field set as the first field on the subform in the tab order. Then in the OnClick, Me!subformname.SetFocus

  3. #3
    dhicks is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    36
    Hi, I have the field set as 0 in the tab order, but the command isn't working for me. I tried it with brackets around the subform name as well.

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I see you have 2016, I wonder if there is any difference. This works for me as stated. Post a copy of your database if you wish.

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Is your syntax the same as mine in post #2? I have seen it set to use "." instead of "!" but that doesn't work for me.

  6. #6
    dhicks is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    36
    Here is the code for the command button. I have commented out the troublesome line. I've tried several different ways to get it to work, resulting in various runtime errors:

    Private Sub cmdNewChkOut_Click()


    Me.[frmChckOutHist].SetFocus
    DoCmd.GoToRecord , , acNewRec
    Me.[frmChckOutHist].Form.Service_Tag_SN = Me.Service_Tag_SN
    Me.[frmChckOutHist].Form.Reservation_Date = Date
    'Me.frmChckOutHist![Expected_Pickup_Date].SetFocus

    End Sub

    Many thanks!

  7. #7
    dhicks is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    36
    I tried it with both the "." and the "!". The subform is actually a datasheet and I want to set a specific field in the new record to get the focus. Google searches have not been much help as I can find nothing that addresses Access 2016.

  8. #8
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I tried various combinations as well. The only time it works for me is with Me!subform.Setfocus when the name of the object and the Source Object are the same.

  9. #9
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I copied your code and it works perfectly!

  10. #10
    dhicks is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    36
    Well, now I'm REALLY confused!!

  11. #11
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Post your database.

  12. #12
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    If this [frmChckOutHist] is the name of the subform control (and not the subform), then the .Form part is the child of the subform control. Then Reservation_Date is the name of a control on the form. As you can see, it's very hierarchical. I can't see how this
    Me.frmChckOutHist![Expected_Pickup_Date].SetFocus would work. I use this syntax for subform control referencing:
    Code:
    [Forms]![Main form name]![subform control name].[Form]![control name on subform]
    I do not use Me.
    So I would probably write
    Code:
    [Forms]![NameOfFormButNotMe]![frmChckOutHist].[Form]![Expected_Pickup_Date].SetFocus
    I believe you could also use the collection object reference syntax (Forms and Controls are collections) and write
    Code:
    Forms("MainFormName").Controls("subformControlName").Form.Expected_Pickup_Date
    as long as there were no spaces or special characters in the control name. If there were, you'd need square brackets I think.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    dhicks is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    36
    I tried both of your examples, and got runtime error 438 on both of them. Very frustrating. And unfortunately due to company security policies, I cannot post the database. I'm sure I could live without the feature, but I feel like I'm on a quest now. As a very rusty programmer trying to re-learn the skills, I want to see this through. My thought is that the subform is only holding a separate table in datasheet form, the fields are not considered to be individual controls. Therefore, I can't reference the field that I want to set focus to. Is this logic correct?

  14. #14
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    In copying your code, it worked for me. The control on the subform was not referenced, it was the subform object itself. It made no difference how the form was defined - continuous, datasheet or single. That being said, if you go into design view of your subform you will notice that each field is a control, regardless of how the form is displayed in form view, and can be referenced.

    To post your database, provide only a sample of the data and in only the tables required. Change the pertinent values to a,b,c,etc. 10 records should be enough.

  15. #15
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    As I attempt to apprise everyone with my signature, doesn't work doesn't help. Neither does "I tried, but..."

    It takes as little as a missing or misplaced character to cause a problem. The error number suggests you haven't drilled down to the proper level before attempting to apply something to it. The code itself is insufficient; we have to understand what the names apply to.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-22-2017, 07:25 AM
  2. Replies: 1
    Last Post: 02-05-2017, 07:44 AM
  3. Replies: 15
    Last Post: 10-08-2015, 02:24 AM
  4. SetFocus on last added form control in subform
    By faythe1215 in forum Forms
    Replies: 12
    Last Post: 03-02-2015, 04:09 PM
  5. SetFocus on Field in subform
    By quicova in forum Forms
    Replies: 5
    Last Post: 09-20-2013, 04:10 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