我有一些vbscript将行从一张纸复制到另一张,调整编辑并添加新行。
这样可以正常工作,直到它到达一个具有自动编号列的表,当尝试复制时将值“7”复制到要添加“10”的自动编号中,它显然无法正常工作。
所以我试图让它不要尝试从以前的数据库复制这个字段,而是让它自动填充该值并复制其余的数据。这是我的代码:
Public Function update6()
'Temp field
Dim fField As Field
Dim bCopy As Boolean
'Open source database
Dim dSource As Database
Set dSource = CurrentDb
'Open dest database
Dim dDest As Database
Set dDest = DAO.OpenDatabase("C:\Users\BMcDoanld\Documents\SellerDeck 2013\Sites\Site1\ActinicCatalog.mdb")
'Open source recordset
Dim rSource As Recordset
Set rSource = dSource.OpenRecordset("OrderMail", dbOpenForwardOnly)
'Open dest recordset
Dim rDest As Recordset
Set rDest = dDest.OpenRecordset("OrderMail", dbOpenDynaset)
'Loop through source recordset
While Not rSource.EOF
'Reset copy flag
bCopy = False
'Look for record in dest recordset
rDest.FindFirst "nMailID = " & rSource.Fields("nMailID") & ""
If rDest.NoMatch Then
'If not found, copy record
rDest.AddNew
bCopy = True
Else
'If found, check for differences
For Each fField In rSource.Fields
If rDest.Fields(fField.Name) <> rSource.Fields(fField.Name) Then
rDest.Edit
bCopy = True
Exit For
End If
Next fField
Set fField = Nothing
End If
'If copy flag is set, copy record
If bCopy Then
For Each fField In rSource.Fields
rDest.Fields(fField.Name) = rSource.Fields(fField.Name)
Next fField
Set fField = Nothing
rDest.Update
End If
'Next source record
rSource.MoveNext
Wend
'Close dest recordset
rDest.Close
Set rDest = Nothing
'Close source recordset
rSource.Close
Set rSource = Nothing
'Close dest database
dDest.Close
Set dDest = Nothing
'Close source database
dSource.Close
Set dSource = Nothing
End Function
我收到错误的一行是:
'If copy flag is set, copy record
If bCopy Then
For Each fField In rSource.Fields
rDest.Fields(fField.Name) = rSource.Fields(fField.Name)
Next fField
Set fField = Nothing
rDest.Update
End If
我一直在玩检测属性,但是我收到了以下代码无效的属性错误:
For Each fField In rSource.Fields
If rDest.Fields(fField.Name).Properties("ISAUTOINCREMENT") = True Then
rDest.Fields(fField.Name) = rSource.Fields(fField.Name)
End If
答案 0 :(得分:2)
检查字段的Attributes
属性。对于自动编号字段(Attributes And dbAutoIncrField) = dbAutoIncrField
。
由于您要更新除自动编号以外的所有字段,请限制<> dbAutoIncrField
所在的字段。
For Each fField In rSource.Fields
If (fField.Attributes And dbAutoIncrField) <> dbAutoIncrField Then
rDest.Fields(fField.Name) = rSource.Fields(fField.Name)
End If
Next fField