Me alternative Reference in a Public Function

padlocked17

Registered User.
Local time
Yesterday, 22:17
Joined
Aug 29, 2007
Messages
275
Just as the title describes, I need to figure out how to replace "Me" in a public function.

The code is below and I need to rereference where Me.Undo is to something that will work for each instance on every form where I call this public Error Checking function.

Code:
    For Each ctl In TheForm
        If ctl.Tag = "Required" Then
            If ctl = "" Or IsNull(ctl) Then
                Num = 1
                Exit For
            End If
        End If
    Next ctl
    'Oops, looks like the user missed a field
    If Num = 1 Then
        If MsgBox("Data is required in " & ctl.Name & "," & vbCrLf & _
            "You haven't entered required data. Do you want to enter it now?", vbQuestion + vbYesNo, "Required information") = vbYes Then
        Else
            'This will delete all changes on the form to a before "dirty" state
            'Make sure it will Undo AND not allow a form to close without discarding changes
            [COLOR="Red"]Me.Undo[/COLOR]
            'Forms(Screen.ActiveForm).Undo
        End If
        RequiredData = True
    Else
        RequiredData = False
    End If
 
Yep, sorry for not including everything. The actual module's code is:

Code:
Public Function RequiredData(ByVal TheForm As Form) As Boolean
'If RequiredData(Me) Then Cancel = -1
'-----------------------------------------------------------------------------------
'
'Now to call the code, you can use the following in the Before Update event of the Form:
'
'-----------------------------------------------------------------------------------
'Private Sub Form_BeforeUpdate(Cancel As Integer)
'    If RequiredData(Me) Then Cancel = -1
'End Sub


'Check that all controls have required data entered if the tag field has "Required" in it.


    Dim ctl As Control
    Dim Num As Integer
    
    On Error GoTo Err_RequiredData
    
    RequiredData = False
    Num = 0
    
    For Each ctl In TheForm
        If ctl.Tag = "Required" Then
            If ctl = "" Or IsNull(ctl) Then
                Num = 1
                Exit For
            End If
        End If
    Next ctl
    'Oops, looks like the user missed a field
    If Num = 1 Then
        If MsgBox("Data is required in " & ctl.Name & "," & vbCrLf & _
            "You haven't entered required data. Do you want to enter it now?", vbCritical + vbYesNo, "Required information") = vbYes Then
        ctl.SetFocus
        ctl.BackColor = vbRed
        Else
            'This will delete all changes on the form to a before "dirty" state
            'Forms(Screen.ActiveForm).Undo
            
        End If
'        MsgBox "Data is required in " & ctl.Name & "," & vbCr & _
'        "please ensure this is entered.", _
'        vbInformation, "Required Data..."
        RequiredData = True
    Else
        RequiredData = False
    End If

Exit_RequiredData:

    On Error Resume Next
        If Not (ctl Is Nothing) Then
            Set ctl = Nothing
        End If
    Exit Function

Err_RequiredData:

    Select Case Err
        Case 0
            Resume Next
        Case Else
            MsgBox "Error: " & Err.Number & vbCrLf & vbCrLf & Err.Description, _
            vbInformation
    End Select

End Function

And the code I'm using for each form is:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Call the function in the Error Checking Module
    If RequiredData(Me) Then Cancel = -1
End Sub
 

Users who are viewing this thread

Back
Top Bottom