Debugging Error Invalid use of Me Keword

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abest
    New Member
    • Apr 2010
    • 2

    Debugging Error Invalid use of Me Keword

    I am trying to get my form to autofill the values from the last record to the new record. In the form properites on the BeforeInsert, I used the following module

    I have been away from VB for several years and am finding it challenging to understand. Can you all help? The other part of the code debugs. See below

    I keep getting the same error when I debug this
    Compile Error: Invalid use of Me Keyword
    Here is the code. What comes back highlighted is the 3rd line, the word Me

    Module7
    Code:
    Option Compare Database
    
    Private Sub Form_BeforeInsert(Cancel As Integer)
      Dim strMsg As String
      Call CarryOver(Me, strMsg)
      If strMsg <> vbNullString Then
        MsgBox strMsg, vbInformation
      End If
      
    End Sub
    Module9
    Code:
    Option Compare Database
    
    Public Function CarryOver(frm As Form, strErrMsg As String, ParamArray avarExceptionList()) As Long
    On Error GoTo Err_Handler
        'Purpose: Carry over the same fields to a new record, based on the last record in the form.
        'Arguments: frm               = the form to copy the values on.
        '           strErrMsg         = string to append error messages to.
        '           avarExceptionList = list of control names NOT to copy values over to.
        'Return:    Count of controls that had a value assigned.
        'Usage:     In a form's BeforeInsert event, excluding Surname and City controls:
        '               Call CarryOver(Me, strMsg, "Surname", City")
        Dim rs As DAO.Recordset         'Clone of form.
        Dim ctl As Control              'Each control on form.
        Dim strForm As String           'Name of form (for error handler.)
        Dim strControl As String        'Each control in the loop
        Dim strActiveControl As String  'Name of the active control. Don't assign this as user is typing in it.
        Dim strControlSource As String  'ControlSource property.
        Dim lngI As Long                'Loop counter.
        Dim lngLBound As Long           'Lower bound of exception list array.
        Dim lngUBound As Long           'Upper bound of exception list array.
        Dim bCancel As Boolean          'Flag to cancel this operation.
        Dim bSkip As Boolean            'Flag to skip one control.
        Dim lngKt As Long               'Count of controls assigned.
    
        'Initialize.
        strForm = frm.Name
        strActiveControl = frm.ActiveControl.Name
        lngLBound = LBound(avarExceptionList)
        lngUBound = UBound(avarExceptionList)
    
        'Must not assign values to the form's controls if it is not at a new record.
        If Not frm.NewRecord Then
            bCancel = True
            strErrMsg = strErrMsg & "Cannot carry values over. Form '" & strForm & "' is not at a new record." & vbCrLf
        End If
        'Find the record to copy, checking there is one.
        If Not bCancel Then
            Set rs = frm.RecordsetClone
            If rs.RecordCount <= 0& Then
                bCancel = True
                strErrMsg = strErrMsg & "Cannot carry values over. Form '" & strForm & "' has no recrods." & vbCrLf
            End If
        End If
    
        If Not bCancel Then
            'The last record in the form is the one to copy.
            rs.MoveLast
            'Loop the controls.
            For Each ctl In frm.Controls
                bSkip = False
                strControl = ctl.Name
                'Ignore the active control, those without a ControlSource, and those in the exception list.
                If (strControl <> strActiveControl) And HasProperty(ctl, "ControlSource") Then
                    For lngI = lngLBound To lngUBound
                        If avarExceptionList(lngI) = strControl Then
                            bSkip = True
                            Exit For
                        End If
                    Next
                    If Not bSkip Then
                        'Examine what this control is bound to. Ignore unbound, or bound to an expression.
                        strControlSource = ctl.ControlSource
                        If (strControlSource <> vbNullString) And Not (strControlSource Like "=*") Then
                            'Ignore calculated fields (no SourceTable), autonumber fields, and null values.
                            With rs(strControlSource)
                                If (.SourceTable <> vbNullString) And ((.Attributes And dbAutoIncrField) = 0&) _
                                    And Not IsNull(.Value) Then
                                    If ctl.Value = .Value Then
                                        'do nothing. (Skipping this can cause Error 3331.)
                                    Else
                                        ctl.Value = .Value
                                        lngKt = lngKt + 1&
                                    End If
                                End If
                            End With
                        End If
                    End If
                End If
            Next
        End If
    
        CarryOver = lngKt
    
    Exit_Handler:
        Set rs = Nothing
        Exit Function
    
    Err_Handler:
        strErrMsg = strErrMsg & Err.Description & vbCrLf
        Resume Exit_Handler
    End Function
    
    Public Function HasProperty(obj As Object, strPropName As String) As Boolean
        'Purpose: Return true if the object has the property.
        Dim varDummy As Variant
    
        On Error Resume Next
        varDummy = obj.Properties(strPropName)
        HasProperty = (Err.Number = 0)
    End Function
    Last edited by NeoPa; May 2 '10, 01:26 PM. Reason: Please use the [CODE] tags provided
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    1. In the future, kindly use Code Tags for readability purposes.
    2. I just browsed at your code for a minute, but the syntax for passing the Current Form appears to be OK.
    3. Try two things before we proceed any further:
      • Pass the Absolute Reference to the Form as the Function Argument, namely:
        Code:
        Call Carry_Over(Forms("<Form Name>"), strMsg)
      • Explicitly Declare the Parameter in the Function Declaration as Access.Form, namely:
        Code:
        Public Function CarryOver(frm As Access.Form, strErrMsg As String, ParamArray avarExceptionList()) As Long
      • I do not see the ParamArray() Values being passed to the Function.
    4. Let us know how you make out.

    Comment

    • Abest
      New Member
      • Apr 2010
      • 2

      #3
      Thank you for your help. Again I am just getting back to Access and am finding the terms confusing. When I add code above and run, it still stops at Me This seems to be a sticking point I can't get around.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by Abest
        Thank you for your help. Again I am just getting back to Access and am finding the terms confusing. When I add code above and run, it still stops at Me This seems to be a sticking point I can't get around.
        Code:
        Call CarryOver(Me, strMsg)
        1. What is the Value of strMsg being passed?
        2. Where are the ParamArray() Values being passed?

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32661

          #5
          I would guess your problem is that module7 is not a form module. The Me keyword, referring as it does to the associated form - IE the form the module is a part of, is only available in form modules. Standard modules could not sensibly have any meaning for Me, so they cannot use it.

          Welcome to Bytes!

          Comment

          • robjens
            New Member
            • Apr 2010
            • 37

            #6
            Is Paramarray a optional argument? I don't see that statement anywhere. Either way, nothing wrong with passing reference of any object from a form class to a function in a code module as long as your scopes are correct which seems to be the case.

            Comment

            Working...