On Deactivate (1 Viewer)

bconner

Registered User.
Local time
Today, 09:43
Joined
Dec 22, 2008
Messages
183
I have a subform and I have checks I want to do when the user clicks anywhere off of the subform and I am using OnDeactivate.
The problem is the OnDeactivate won't trigger when I click on the parent form or another subform.
I even tried OnlostFocus and it doesn't work either.

Code:
Private Sub Form_Deactivate()


' Check to make sure Outcomes is populated
If (IsNull(cmb_Action_Outcome) = True And IsNull(cmb_Action_Taken) = False) Then
   MsgBox "Action Outcome is a required field, please choose an Action Outcome."
   cmb_Action_Outcome.SetFocus
   Exit Sub
End If
' Check to make sure the action taken is populated
If (IsNull(cmb_Action_Outcome) = False And IsNull(cmb_Action_Taken) = True) Then
   MsgBox "Action Taken is a required field, please choose an Action Taken."
   cmb_Action_Outcome = Null
   cmb_Action_Taken.SetFocus
   Exit Sub
End If

End Sub
 

June7

AWF VIP
Local time
Today, 06:43
Joined
Mar 9, 2014
Messages
5,474
You want to do some checks even if data has not been entered/edited?
 

Mike Krailo

Well-known member
Local time
Today, 10:43
Joined
Mar 28, 2020
Messages
1,044
The problem is the OnDeactivate won't trigger when I click on the parent form or another subform.
I even tried OnlostFocus and it doesn't work either.
I'll bet it works if you put this in the subforms before update event.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   ' Check to make sure Outcomes is populated
   If (IsNull(cmb_Action_Outcome) = True And IsNull(cmb_Action_Taken) = False) Then
      MsgBox "Action Outcome is a required field, please choose an Action Outcome."
      Cancel = True
      cmb_Action_Outcome.SetFocus
      Exit Sub
   End If
   ' Check to make sure the action taken is populated
   If (IsNull(cmb_Action_Outcome) = False And IsNull(cmb_Action_Taken) = True) Then
      MsgBox "Action Taken is a required field, please choose an Action Taken."
      Cancel = True
      cmb_Action_Taken.SetFocus
      Exit Sub
   End If
End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:43
Joined
Feb 28, 2001
Messages
27,191
Not surprised that a Form_LostFocus doesn't fire very often, since a form can ONLY have focus if NONE of its controls are active (or are not capable of gaining focus in the first place.) A control_LostFocus should work OK, though.
 

moke123

AWF VIP
Local time
Today, 10:43
Joined
Jan 11, 2013
Messages
3,920
If (IsNull(cmb_Action_Outcome) = True And IsNull(cmb_Action_Taken) = False) Then
If (IsNull(cmb_Action_Outcome) = False And IsNull(cmb_Action_Taken) = True) Then
Do you need the "And" portion? What if both are null?

You can also use a public function to validate your forms.
In the Before update event
Code:
Cancel = InvalidFrm(me)
In the tag property of the controls to check put V8
The control must have a label (used in a msgbox) but it can be hidden if desired.
Fires a message box listing missing info and highlights controls in red.

Code:
Public Function InValidFrm(frm As Form) As Boolean
    'returns True if missing data  ie. Cancel = InvalidFrm
    'control requires a label, can be hidden if desired
    'Place V8 in tag of control to test for missing info

    Dim ctl As Control, ctlSub As Control
    Dim msg As String
      
    For Each ctl In frm.Controls
    
        If InStr(1, ctl.Tag, "V8") Then

            If Nz(ctl.Value, "") = "" Then
                InValidFrm = True
                msg = msg & Space(5) & "* " & ctl.Controls(0).Caption & vbNewLine
                ctl.BorderColor = vbRed
            Else
                ctl.BorderColor = vbBlack
            End If
            
        ElseIf TypeName(ctl) = "Subform" Then

            For Each ctlSub In ctl.Form.Controls
                
                If InStr(1, ctlSub.Tag, "V8") Then

                    If Nz(ctlSub.Value, "") = "" Then
                        InValidFrm = True
                        msg = msg & Space(5) & "* " & ctlSub.Controls(0).Caption & vbNewLine
                        ctlSub.BorderColor = vbRed
                    Else
                        ctlSub.BorderColor = vbBlack

                    End If
            
                End If

            Next

        End If
      
    Next
    
    If InValidFrm Then
        MsgBox Space(6) & "Please Complete The " & vbNewLine & "Following Required Fields:" & vbNewLine & vbNewLine & msg, vbExclamation, "Missing Info!"
    End If
    
End Function

V8.jpg
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:43
Joined
Feb 19, 2002
Messages
43,294
I have a subform and I have checks I want to do when the user clicks anywhere off of the subform
As @Mike Krailo alluded - if you are trying to validate data, you MUST do it in the correct event in order for it to be effective.

You might benefit from viewing these videos and playing with the database that is used to show form and control events

 
Last edited:

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 20:13
Joined
Mar 22, 2009
Messages
784
Please mark the thread "Solved" if you have found the solution...
 

Users who are viewing this thread

Top Bottom