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



我有一个函数,每当用户对记录进行更改时,它都会将条目写入表中。 但是,函数运行后,每当我尝试在代码的任何部分引用Me.Recordset对象或方法时,我都会得到error 3420 'Object invalid or no longer'。 如果我重新启动 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

查看您在使用Me.Recordset时遇到问题的其他代码可能会有所帮助。 但首先看看这些变化是否改善了这种情况。

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

最新更新