如何修复“ 438-对象不支持属性或方法错误”

时间:2019-01-08 18:55:59

标签: access-vba

我已经在我的访问数据库中包含了一个审计跟踪代码,可以将其称为2种不同形式。该代码在其中一种形式下可以正常工作,但在另一种形式下会产生438错误。

-使用相同的参数以两种形式调用代码 -调试器突出显示以下行:'如果Nz(ctl.Value)<> Nz(ctl.OldValue)然后 -我试图注释掉调用该过程的代码,问题似乎出在参数“ SingleName”上 -我已经检查了文本框的“控件来源”和“名称”,并且都显示正确。

Sub AuditChanges(IDField As String, UserAction As String)
    On Error GoTo AuditChanges_Err
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim ctl As Control
    Dim datTimeCheck As Date
    Dim strUserID As String
    Set cnn = CurrentProject.Connection
    Set rst = New ADODB.Recordset
    rst.Open "SELECT * FROM TBL_AuditTrail", cnn, adOpenDynamic, 
adLockOptimistic
    datTimeCheck = Now()
    strUserID = Environ("USERNAME")
    Select Case UserAction
        Case "EDIT"
            For Each ctl In Screen.ActiveForm.Controls
                If ctl.Tag = "Audit" Then
                    If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
                        With rst
                            .AddNew
                            ![DateTime] = datTimeCheck
                            ![UserName] = strUserID
                            ![FormName] = Screen.ActiveForm.Name
                            ![Action] = UserAction
                            ![RecordID] = 
Screen.ActiveForm.Controls(IDField).Value
                            ![FieldName] = ctl.ControlSource
                            ![OldValue] = ctl.OldValue
                            ![NewValue] = ctl.Value
                           .Update
                        End With
                    End If
                End If
           Next ctl
        Case Else
            With rst
               .AddNew
               ![DateTime] = datTimeCheck
               ![UserName] = strUserID
               ![FormName] = Screen.ActiveForm.Name
               ![Action] = UserAction
               ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
                .Update
            End With
    End Select
AuditChanges_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
AuditChanges_Err:
    MsgBox Err.Number & Err.Description
    Resume AuditChanges_Exit
End Sub





Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
        Call AuditChanges("SingleName", "NEW")
    Else
        Call AuditChanges("SingleName", "EDIT")
    End If
End Sub

该表单的BeforeUpdate事件应该调用该过程并将任何更改,删除或添加发送到TBL_AuditTrail。 输入数据并尝试保存后,会发生438错误。 信息仍然发送到表(TBL_AuditTrail)

2 个答案:

答案 0 :(得分:1)

未绑定的控件没有 OldValue 属性。您可以检查一下:

If ctl.ControlSource <> "" Then
    ![OldValue].Value = ctl.OldValue
Else
    ' Skip unbound control.
End If

答案 1 :(得分:0)

没有看到有问题的三种形式,我只能说Screen.ActiveForm.Controls(IDField)字段有所不同。我将比较所有三个字段的属性,以查看失败的字段有何不同。