MS Access DAO 记录集更新不起作用

MS Access DAO recordset update not working

我制作了一个 MS Access 数据库(旧的 XP 版本),它曾经无缝工作。

我不得不向 "move" 添加一个例程,从 table 到另一个 "move" 一些数据,并编写了一个函数来执行此操作。 我一直使用的方法是使用动态记录集(或动态集),但这次不起作用。 流程正确打开动态集,查找数据并将其从一个记录集复制到另一个记录集,但是当完成 .update 后,原始 table.

中什么也没有出现

我使用 DAO 3.60。

这是(总结的)代码:

On Error Resume Next

Dim rstDoc As Recordset
Dim rstAdd As Recordset
Dim rstDocEmessi As Recordset
Dim rstAddDocEmessi As Recordset
Dim Incassato As Integer

Set rstDoc = CurrentDb.OpenRecordset("Documenti", dbOpenSnapshot)
Set rstDocEmessi = CurrentDb.OpenRecordset("TS_DocumentiEmessi", dbOpenDynaset)
Set rstAdd = CurrentDb.OpenRecordset("Addebiti", dbOpenDynaset)
Set rstAddDocEmessi = CurrentDb.OpenRecordset("TS_Addebiti_DocumentiEmessi", dbOpenDynaset)

numDoc = Forms!TS_SceltaStampa!IdDocumento

    With rstDocEmessi

            rstDocEmessi.AddNew

            rstDocEmessi!IdDocOriginale = rstDoc!IdDocumento
            rstDocEmessi!Data = rstDoc!Data
            rstDocEmessi![#Fattura] = rstDoc![#Fattura]
            ...
            rstDocEmessi!TS_Opposizione = rstDoc!TS_Opposizione
            rstDocEmessi!TS_DataPagamento = rstDoc!TS_DataPagamento
            rstDocEmessi!IsIncassato = (IIf(Incassato = vbYes, True, False))
            rstDocEmessi!IsImportatoInSospesi = False

            rstDocEmessi.Update
            rstDocEmessi.Close

            ' Copia Addebiti
            If Not (rstAdd.EOF And rstAdd.BOF) Then
                rstAdd.MoveFirst
                Do Until rstAdd.EOF = True
                    If rstAdd!Documento = numDoc Then

                        rstAddDocEmessi.AddNew

                        rstAddDocEmessi!IdAddebito = rstAdd!IdAddebito
                        rstAddDocEmessi!Documento = rstAdd!Documento
                        ...
                        rstAdd!TS_TipoSpesa
                        rstAddDocEmessi!Calcola = rstAdd!Calcola
                        rstAddDocEmessi!Totale = rstAdd!Totale

                        rstAddDocEmessi.Update
                    End If
                    rstAdd.MoveNext
                Loop
            End If
            rstAddDocEmessi.Close
            rstAdd.Close
            TS_Registra = True`

我有几点建议。

首先不要使用 On Error Resume Next 除非你期望在一行代码中出现特定错误,你将在下一行代码中显式测试和处理(通过测试 If Err.Number = ...).您应该有一个错误处理代码块并使用 On Error GoTo ERROR_CODE_BLOCK。如果您要为一个特定的命令关闭错误处理程序,那么您应该在处理完预期的错误后立即重新打开它。

因为您关闭了错误处理,可能是您的插入语句由于某些约束违规而失败,但您只是没有看到这一点。对于错误处理,我建议您像这样构建代码:

On Error GoTo PROC_ERR

    Dim rstDoc As Recordset
    '...
    'insert the body of your Procedure here
    '...

PROC_EXIT:
    'Add any tidying up code that always needs to run. For example, release all your Object variables
    Set rstDoc = Nothing
    Set rstAdd = Nothing
    Set rstDocEmessi = Nothing
    Set rstAddDocEmessi = Nothing
    Exit Sub

PROC_ERR:
    MsgBox "Error " & Err.Number & " - " & Err.Description
    Resume PROC_EXIT

End Sub

一般代码整理建议。

With rstDocEmessi 构造用于为您节省一些输入。您的代码中应该有一个关联的 End With,但我没有看到。我会按如下方式更改这段代码:

With rstDocEmessi
    .AddNew
        !IdDocOriginale = rstDoc!IdDocumento
        !Data = rstDoc!Data
        ![#Fattura] = rstDoc![#Fattura]
        ...
        !TS_Opposizione = rstDoc!TS_Opposizione
        !TS_DataPagamento = rstDoc!TS_DataPagamento
        !IsIncassato = (IIf(Incassato = vbYes, True, False))
        !IsImportatoInSospesi = False
    .Update
    .Close
End With

最后,rstAddDocEmessi 中的插入可以稍微清理一下。与其打开 rstAdd 的整个 table 记录,然后依次检查每条记录以查看是否需要添加 rstAddDocEmessi 记录,不如直接在 rstAddDocEmessi 中获取相关记录=19=] 记录集?

Set rstAdd = CurrentDb.OpenRecordset("Select * From Addebiti " & _
        "Where Documento = " & Forms!TS_SceltaStampa!IdDocumento, dbOpenDynaset)

'No need to test for (rstAdd.BOF And rstAdd.EOF), and no need for rstAdd.MoveFirst
'Just go straight into...
Do Until rstAdd.EOF = True
    rstAddDocEmessi.AddNew
        rstAddDocEmessi!IdAddebito = rstAdd!IdAddebito
        rstAddDocEmessi!Documento = rstAdd!Documento
        ...
        rstAddDocEmessi!Calcola = rstAdd!Calcola
        rstAddDocEmessi!Totale = rstAdd!Totale
    rstAddDocEmessi.Update
    rstAdd.MoveNext
Loop