office交流網(wǎng)--QQ交流群號(hào)及微信交流群

Access培訓(xùn)群:792054000         Excel免費(fèi)交流群群:686050929          Outlook交流群:221378704    

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

微信交流群(請(qǐng)用微信掃碼)

        

access復(fù)制父窗體和子窗體的選擇的記錄

2019-09-05 15:14:00
tmtony8
原創(chuàng)
3726

在應(yīng)用程序中,我們有時(shí)需要復(fù)制父子窗體選中的記錄

比如復(fù)制訂單記錄。包括訂單信息,訂單明細(xì)信息,詳細(xì)代碼如下:

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.客戶
                !發(fā)貨日期 = Me.發(fā)貨日期
                '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 訂單明細(xì) ( 訂單id, 訂單明細(xì)id, 數(shù)量, 商品, 單價(jià) ) " & _
                    "SELECT 訂單id, 訂單明細(xì)id, 數(shù)量, 商品, 單價(jià) " & _
                    "FROM 訂單明細(xì) 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



此時(shí)訂單和訂單明細(xì)中都能復(fù)制添加上記錄


    分享
    文章分類
    聯(lián)系我們
    聯(lián)系人: 王先生
    Email: 18449932@qq.com
    QQ: 18449932
    微博: officecn01
    移動(dòng)訪問