我制作了MS Access DB(旧的XP版本),它曾经无缝地工作。
我必须添加一个例程来"移动"从表到另一个表的一些数据,并写了一个函数来做到这一点。 我一直使用的方法是使用动态记录集(或动态集),但这次它不起作用。 流正确打开动态集,查找数据并将数据从一个记录集复制到另一个记录集,但是当完成.update时,原始表中没有任何内容。
我使用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`
答案 0 :(得分:0)
我有一些建议。
首先不要使用On Error Resume Next
,除非您希望在下一行代码中明确测试和处理的代码行中出现特定错误(通过测试{{1} }})。您应该有一个错误处理代码块并使用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
记录,为什么不在您的rstAddDocEmessi
中获取相关记录记录?
rstAdd