【发布时间】:2020-10-27 19:48:03
【问题描述】:
在下面的代码中,我试图创建与单个 JobID 相关的所有记录的副本。这是为了修订目的创建具有新 ID 的相同作业。代码是:
Public Function CreateRevisions()
Dim lngOldID As Long 'change from long if text
Dim lngNewID As Long
Dim rsS As Recordset 'source recordset
Dim rsT As Recordset 'target recordset
Dim fld As Field
DoCmd.RunCommand acCmdSaveRecord 'Save the record, to make sure all data is saved in table
lngOldID = Forms!JobQuote!JobID
'Copy the main table
Set rsS = CurrentDb.OpenRecordset("Select * From tblJobDetails where JobID=" & lngOldID, dbOpenSnapshot)
Set rsT = CurrentDb.OpenRecordset("tblJobDetails", dbOpenDynaset, dbAppendOnly)
rsT.AddNew
For Each fld In rsS.Fields
rsT.Fields(fld.Name) = fld
Next
lngNewID = rsT!JobID
rsT.Update
'Copies Drawings
Set rsS = CurrentDb.OpenRecordset("Select * From tblDrawings where JobID=" & lngOldID, dbOpenSnapshot)
Set rsT = CurrentDb.OpenRecordset("tblDrawings", dbOpenDynaset, dbAppendOnly)
rsT.AddNew
rsT!JobID = lngNewID
For Each fld In rsS.Fields
If fld.Name <> "JobID" Then
rsT.Fields(fld.Name) = fld
End If
Next
rsT.Update
“复制图纸”部分会重复多次,但会针对不同的表格进行。我在 rsT.Fields(fld.Name) = fld 上遇到运行时错误。这是运行时错误64224: Method of 'Value' of object 'Field2' failed。
为什么会出现这个错误?
【问题讨论】:
-
您可以调试代码添加断点并使用
F8。只是检查一下,表中是否有任何多值字段?检查这个问题stackoverflow.com/q/49245029/1521579