在下面的代码中,我试图创建与单个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
。
为什么会出现该错误?
答案 0 :(得分:2)
首先,您可能缺少将这些对象指定为DAO对象的功能,并且在检索新ID之前应更新记录。
第二,使用手头的RecordsetClone可能更简单,更快。
这是来自工作表单和子表单的实际代码:
Private Sub CopyButton_Click()
Dim rst As DAO.Recordset
Dim rstAdd As DAO.Recordset
Dim fld As DAO.Field
Dim Count As Integer
Dim Item As Integer
Dim Bookmark As Variant
Dim OldId As Long
Dim NewId As Long
' Copy parent record.
Set rstAdd = Me.RecordsetClone
Set rst = rstAdd.Clone
' Move to current record.
rst.Bookmark = Me.Bookmark
OldId = rst!Id.Value
With rstAdd
.AddNew
For Each fld In .Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
Else
.Value = rst.Fields(.Name).Value
End If
End With
Next
.Update
' Pick Id of the new record.
.MoveLast
NewId = !Id.Value
End With
' Store location of new record.
Bookmark = rstAdd.Bookmark
' Copy child records 1.
' If a subform is present:
' Set rstAdd = Me!subChild1.Form.RecordsetClone
' If a subform is not present, retrieve records from the child table:
Set rstAdd = CurrentDb.OpenRecordset("Select * From tblChild1 Where FK = " & OldId & "")
Set rst = rstAdd.Clone
If rstAdd.RecordCount > 0 Then
rstAdd.MoveLast
rstAdd.MoveFirst
End If
Count = rstAdd.RecordCount
For Item = 1 To Count
With rstAdd
.AddNew
For Each fld In .Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "FK" Then
' Skip master/child field.
.Value = NewId
Else
.Value = rst.Fields(.Name).Value
End If
End With
Next
.Update
End With
rst.MoveNext
Next
' Copy child records 2.
' If a subform is present:
Set rstAdd = Me!subChild2.Form.RecordsetClone
' If a subform is not present, retrieve records from the child table:
' Set rstAdd = CurrentDb.OpenRecordset("Select * From tblChild2 Where FK = " & OldId & "")
Set rst = rstAdd.Clone
If rstAdd.RecordCount > 0 Then
rstAdd.MoveLast
rstAdd.MoveFirst
End If
Count = rstAdd.RecordCount
For Item = 1 To Count
With rstAdd
.AddNew
For Each fld In .Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "FK" Then
' Skip master/child field.
.Value = NewId
Else
.Value = rst.Fields(.Name).Value
End If
End With
Next
.Update
End With
rst.MoveNext
Next
rst.Close
rstAdd.Close
' Move to the new recordcopy.
Me.Bookmark = Bookmark
Set fld = Nothing
Set rstAdd = Nothing
Set rst = Nothing
End Sub
答案 1 :(得分:0)
我尝试了您的代码,除非将记录集和字段对象变量声明为DAO类型,否则它不会编译或运行。
考虑不考虑记录集类型且不需要字段变量的替代方法:
Dim x As Integer
For x = 0 To rsS.Fields.Count - 1
rsT(rsS(i).Name) = rsS(i)
Next