通过访问中表格上的按钮将费用记录复制到发票明细

时间:2019-02-08 11:38:41

标签: ms-access access-vba

这是使用带有子表单的发票输入表单将费用记录中的记录添加到发票明细表中 全部与“ inv no”相关

我正在处理来自 Exp_Inv_input_Form 至 子表单=主表单的服务atlan =亚特兰大inv main

我一直试图在没有运气的情况下使用INSERT INTO,无法弄清楚我要去哪里哪里

    Private Sub btn_copy_Click()

Dim strSql As String
Dim IngID As Long

If Me.Dirty Then
        Me.Dirty = False
    End If

If Me.NewRecord Then
    MsgBox "select the record to duplicate."
Else
    With Me.RecordsetClone
        .AddNew
            !description_date = Me.TransactionDate
            !description = Me.description
            !hours = Me.Quantity
            !Price = Me.SubTotal
            !Total = Me.SubTotal
        .Update

        .Bookmark = .LastModified
        IngID = !inv_no

        If Me.RecordsetClone.RecordCount > 0 Then
            strSql = "insert into [service atlan subform]([inv no], [description date], description, hours, Price, Billed )" _
                "SELECT " & lngID & " As NewID, description, Quantity, Total, from_exp " & _
                "FROM [service atlan] WHERE inv no = " & Me.inv_no & ";"
        DBEngine(0)(0).Execute strSql, dbFailOnError
    Else
        MsgBox "Main record duplicated, but there were no related records."
    End If
Exit_Handler:
    Exit Sub

Err_Handler:
    MsgBox "Error " & Err.Number & " - " & Err.description, , "cmdDupe_Click"
    Resume Exit_Handler
End Sub

现在它告诉我我的strSql有问题 我是否需要在两个表中都用相同的名称命名列,因为我被认为是列的顺序

预先感谢

2 个答案:

答案 0 :(得分:0)

您的sql语句几乎没有问题。如果列名中有空格,请用[]括起来。

insert语句中的列数也与select语句不匹配

,语句中表名之后还有Insert

答案 1 :(得分:0)

我一直在搞弄SLQ,改变了表结构,并且成功了,我最大的问题是Select语句的结构

"INSERT INTO [Transaction List] ([TransactionDate], [Inv No], [Division], [MomsrevLookup], [Total], [Catagory]) " & _
        "Select """ & Me.[TransactionDate].Value & """, """ & Me.[inv no].Value & """,""" & Me.[Division].Value & """,""" & Me.revmoms.Value & """,""" & Me.[Inv_Total].Value & """,""" & Me.[Catagory].Value & """"

现在它正在处理表单中的选定行,并将其粘贴到另一个表上的新记录中