office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

access复制父窗体和子窗体的选择的记录

2019-09-05 15:14:00
tmtony8
原创
3318

在应用程序中,我们有时需要复制父子窗体选中的记录

比如复制订单记录。包括订单信息,订单明细信息,详细代码如下:

Dim strSql As String    'SQL statement.
    Dim lngID As Long       'Primary key value of the new record.
    
    'Save any edits first
    If Me.Dirty Then
        Me.Dirty = False
    End If
    
    'Make sure there is a record to duplicate.
    If Me.NewRecord Then
        MsgBox "Select the record to duplicate."
    Else
        'Duplicate the main record: add to form's clone.
        With Me.RecordsetClone
            .AddNew
                !订单id = Me.订单id
                !客户 = Me.客户
                !发货日期 = Me.发货日期
                'etc for other fields.
            .Update
            
            'Save the primary key value, to use as the foreign key for the related records.
            .Bookmark = .LastModified
            lngID = !ID
            
            'Duplicate the related records: append query.
            If Me.subform.Form.RecordsetClone.RecordCount > 0 Then
                strSql = "INSERT INTO 订单明细 ( 订单id, 订单明细id, 数量, 商品, 单价 ) " & _
                    "SELECT 订单id, 订单明细id, 数量, 商品, 单价 " & _
                    "FROM 订单明细 WHERE 订单id = " & ID & ""
                DBEngine(0)(0).Execute strSql, dbFailOnError
            Else
                MsgBox "Main record duplicated, but there were no related records."
            End If
            
            'Display the new duplicate.
            Me.Bookmark = .LastModified
        End With
    End If

Exit_Handler:
    Exit Sub

Err_Handler:
    MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
    Resume Exit_Handler



此时订单和订单明细中都能复制添加上记录


    分享