2

我有一些 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
4

1 回答 1

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
于 2013-09-13T16:21:51.803 回答