-1

我在 MS Access 中使用 vba 在 Quickbooks 中使用 QODBC 创建发票。此过程需要首先插入多行发票项目并保存临时直到插入主发票信息。我有几张发票需要批量插入。

例子:

MultiLINE (INVOICE ITEMS) = Item #, OrderID, Item Desc, 等等。

**MULTILINE 匹配基于 OrderID 的主发票

主要 (INVOICE) = OrderID、姓名、地址、账单条款等。

**主要是每个 orderID 的单行记录

“QB_AppendInvoice_LoopRef”包含需要处理的唯一 orderid。我试图将其用作记录集以根据当前记录集 orderid 导入多行项目,但是,我无法引用当前记录集 orderid。

Dim db          As DAO.Database
Dim rs          As DAO.Recordset
Dim iCount      As Integer
Set db = CurrentDb()
Set rs = db.OpenRecordset("QB_AppendInvoice_LoopRef") 'open the recordset for use (table, Query, SQL Statement)
   With rs

    If .RecordCount <> 0 Then 'Ensure that there are actually records to work with
        'The next 2 line will determine the number of returned records
        rs.MoveLast 'This is required otherwise you may not get the right count
        iCount = rs.RecordCount 'Determine the number of returned records
        Do While Not .BOF
            DoCmd.SetWarnings False
            'Append Invoice Line (determine tests ordered)
            Dim SQL1 As String
            SQL1 = "INSERT INTO InvoiceLine (CustomerRefListID, CustomerRefFullName, ARAccountRefListID, ARAccountRefFullName, InvoiceLineSerialNumber, InvoiceLineLotNumber, TemplateRefListID, IsPending, DueDate, TxnDate, InvoiceLineType, InvoiceLineItemRefListID, InvoiceLineItemRefFullName, InvoiceLineDesc, InvoiceLineRate, InvoiceLineAmount, FQSaveToCache, RefNumber)" & _
            "SELECT Customer.ListID, Customer.FullName, '4C0000-1070045186', 'Accounts Receivable', Null, Null, '80000023-1495649075', '0', QB_ORDER_DETAILS.OrderDate, QB_ORDER_DETAILS.OrderDate, 'Item', QB_TestList_TestCodes.ListID, QB_TestList_TestCodes.FullName, QB_TestList_TestCodes.Description, QB_TestList_TestCodes.SalesOrPurchasePrice, QB_TestList_TestCodes.SalesOrPurchasePrice, '1', QB_ORDER_DETAILS.OrderID " & _
            "FROM ((Customer INNER JOIN contacts ON Customer.AccountNumber = contacts.Company) INNER JOIN QB_ORDER_DETAILS ON contacts.[Full Member Info] = QB_ORDER_DETAILS.Physician) LEFT JOIN QB_TestList_TestCodes ON QB_ORDER_DETAILS.ProductID = QB_TestList_TestCodes.TestCode " & _
            "WHERE QB_ORDER_DETAILS.OrderID = rs.Fields.getvalue('OrderID')"
            DoCmd.RunSQL SQL1, False

            'Append Invoice to Invoice Line (put the tests ordered on an invoice)
            Dim SQL2 As String
            SQL2 = "INSERT INTO Invoice (CustomerRefListID, CustomerRefFullName, ARAccountRefListID, ARAccountRefFullName, TemplateRefListID, [Memo], IsPending, IsToBePrinted, CustomFieldOther, ItemSalesTaxRefListID, TxnDate, DueDate, RefNumber)" & _
            "SELECT Customer.ListID, Customer.FullName, '4C0000-1070045186', 'Accounts Receivable', '80000023-1495649075', [Patient_Last] & ', ' & [Patient_First] & ' - ' & [Full_Specimen_ID], '0', '0', [Patient_Last] & ', ' & [Patient_First] & ' - ' & [Full_Specimen_ID], Null, [OrderDate], [OrderDate], Orders.OrderID" & _
            "FROM Customer INNER JOIN (Orders INNER JOIN contacts ON Orders.Physician = contacts.[Full Member Info]) ON Customer.AccountNumber = contacts.Company" & _
            "WHERE Orders.OrderID = rs.Fields.getvalue('OrderID')"
            DoCmd.RunSQL SQL2, False
        .MovePrevious
        Loop
    Else
        MsgBox "There are no records in the recordset."
    End If
        MsgBox "SENT TO QB - SUCCESS!!!"

    End With
        rs.Close 'Close the recordset
        Set rs = Nothing 'Clean up
        DoCmd.SetWarnings True

结束子

4

1 回答 1

1

这是因为您正在更新字符串,VBA 不能像那样在字符串中使用变量,这是正确的做法:

"WHERE Orders.OrderID = " & rs.Fields("OrderID").Value

如果值是字符串,则必须添加引号:

"WHERE Orders.OrderID = '" & rs.Fields("OrderID").Value & "'"
于 2017-06-22T18:03:46.217 回答