在函数中使用记录集会导致主子代码中出现错误3420

时间:2013-12-26 20:00:53

标签: ms-access access-vba ms-access-2010

我有一个函数,只要用户对记录进行更改,就会将条目写入表中。但是,在函数运行后,每当我尝试在代码的任何部分引用error 3420 'Object invalid or no longer'对象或方法时,我都会得到Me.Recordset。如果我重新启动Access,问题将重置,直到该功能再次运行。我该怎么做才能防止这种情况发生?代码如下:

    Function fAddAuditRecord(lngSysID As Long, intChangeType As Byte, Optional strComments As String, Optional intAttach As Integer, _
    Optional intNewStatus As Integer)
    Dim blnInTrans As Boolean
    On Error GoTo Err_handle
    Select Case intChangeType   'Select comments based on type of change
        Case 1
            strComments = "Record Added"
        Case 2                  
            strComments = "Record Edited" & strComments
        Case 3                  
            strComments = "Attachment " & intAttach & " Created"
        Case 4                  
            strComments = "Status Changed to " & intNewStatus
    End Select
    Dim WS As DAO.Workspace, sqlNewStep As String, dbNewStep As DAO.Database, intNewStep As Byte
    intNewStep = DCount("[StepNumber]", "tblAuditTrack", "[SystemIDCode] = " & lngSysID) + 1
    Set dbNewStep = CurrentDb
    Set WS = DBEngine.Workspaces(0)
    sqlNewStep = "INSERT INTO tblAuditTrack (SystemIDCode, StepNumber, InputUser, DateCreated, Comments, " _
        & "ChangeType) VALUES (" & lngSysID & ", " & intNewStep & ", '" & strUserName & "', Date(), '" _
        & strComments & "', " & intChangeType & ");"

    WS.BeginTrans
    blnInTrans = True
        dbNewStep.Execute sqlNewStep
    WS.CommitTrans
    blnInTrans = False
    dbNewStep.Close
    WS.Close

1 个答案:

答案 0 :(得分:0)

查看Me.Recordset出现问题的其他代码可能会有所帮助。但首先要看这些变化是否会改善这种情况。

WS.CommitTrans
blnInTrans = False
'dbNewStep.Close
'WS.Close