Results 1 to 5 of 5
  1. #1
    swngdncr is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Posts
    6

    Neither SetFocus or gotoControl working, form with subform

    Uncle, I can't figure this out. I have a form with multiple subforms. On subformA, I want to make sure that if the user checks the control "MatchProject", that they have made an entry in the "MatchingGrant" field. The code to do the validation is working fine, but I want to force the user to go back to either uncheck the box or put a value in "MatchingGrant" before they can move on to another field. This part on BeforeUpdate for the subform works fine:



    'If [MatchProject] = -1 And IsNull(MatchingGrant) Then
    MsgBox "You must enter a matching grant or unselect Match Project"

    But, neither of the below code actually takes curser back to the "txtMatchingGrant" field. It doesn't generate an error, it just doesn't change the focus. I've tried first doing setfocus on the subform then either setfocus or gotocontrol to go back to "txtMatchingGrant"... but it just doesn't do anything. The focus stays wherever I put it when I clicked outside SubformA. Any help would be appreciated.

    Forms!frmCOEACtivities.Enter_Funding_Source_s_.Set Focus
    DoCmd.GoToControl "txtMatchingGrant"
    Me.txtMatchingGrant.SetFocus

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    The code is in which form BeforeUpdate event? The subform?

    Me.txtMatchingGrant.SetFocus works for me.

    Post the complete procedure.
    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.

  3. #3
    swngdncr is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Posts
    6
    Actually, I've tried it in multiple different events, I've put in the "on exit" event on the property sheet for the form where you only have the two options of "on enter" and "on exit", I've done it on before update, after update, lost focus .. I've tried every possible event that I think might work.... It's pretty simple code, below is on the "on Exit" property. I've done this a zillion times before and never had a problem. The subform I'm trying to run this on has a source object of: SubFrmEnterFundingSource but I gave it the caption of "Enter Funding Source(s)" When I use "SubfrmENterFundingSource" in the code, it says it cannot find the form. When I use "Enter_Funding_Source_s_" it doesn't generate the '
    can't find the form error', but it doesn't change the focus. As an experiment, I tried running the same code referencing one of the other subforms and controls on the main form. It works fine there. So, this works:

    Private Sub Enter_Funding_Source_s__Exit(Cancel As Integer)


    If [MatchProject] = -1 And IsNull(MatchingGrant) Then

    MsgBox "You must enter a matching grant or unselect Match Project"
    Undo

    Forms!frmCOEACtivities.FrmProjectCoordinationTime. SetFocus
    DoCmd.GoToControl "resource"

    End If

    End Sub

    ------------------------------
    But this does not work:


    Private Sub Enter_Funding_Source_s__Exit(Cancel As Integer)


    If [MatchProject] = -1 And IsNull(MatchingGrant) Then

    MsgBox "You must enter a matching grant or unselect Match Project"
    Undo

    Forms!frmCOEACtivities.Enter_Funding_Source_s_.Set Focus
    DoCmd.GoToControl "txtMatchingGrant"

    End If

    End Sub

    Quote Originally Posted by June7 View Post
    The code is in which form BeforeUpdate event? The subform?

    Me.txtMatchingGrant.SetFocus works for me.

    Post the complete procedure.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,820
    The OnEnter and OnExit events you describe are for the subform container control, not the form in the container.

    Form BeforeUpdate event is the usual event to validate data before record is committed and then to abort the update if data does not meet conditions.

    For events with a Cancel argument, if you want to abort the action (such as exit or record update) then set the variable to True.

    Example:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.Base) Then
        Cancel = True
        Me.Base.SetFocus
    End If
    End Sub
    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.

  5. #5
    swngdncr is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Posts
    6
    OMG, that worked perfectly! I had tried doing it similar with "undo" instead of "cancel".. and it didn't work.... I knew this should be simple and easy... Thank you so much! -cjr-

    Quote Originally Posted by June7 View Post
    The OnEnter and OnExit events you describe are for the subform container control, not the form in the container.

    Form BeforeUpdate event is the usual event to validate data before record is committed and then to abort the update if data does not meet conditions.

    For events with a Cancel argument, if you want to abort the action (such as exit or record update) then set the variable to True.

    Example:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.Base) Then
        Cancel = True
        Me.Base.SetFocus
    End If
    End Sub

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

Similar Threads

  1. GoToControl Subform
    By bezoomnyveshch in forum Programming
    Replies: 3
    Last Post: 12-06-2016, 09:14 AM
  2. Replies: 1
    Last Post: 03-21-2016, 10:40 AM
  3. SetFocus on last added form control in subform
    By faythe1215 in forum Forms
    Replies: 12
    Last Post: 03-02-2015, 04:09 PM
  4. form.setfocus not really working
    By bbxrider in forum Forms
    Replies: 4
    Last Post: 12-18-2014, 12:47 PM
  5. GoToControl and SetFocus question
    By bgephart in forum Forms
    Replies: 5
    Last Post: 08-29-2012, 12:10 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