2017-06-22 34 views
-1

我正在MS Access中使用vba創建與QODBC Quickbooks中的發票。 此過程需要首先插入多行發票項目並保存臨時文件,直到插入主發票信息。我有幾個需要作爲批量插入的發票。我如何設置2插入查詢基於openrecordset字段ID(使用QODBC&MS ACCESS的進銷存)

實施例:

多(發票項目)=項目#,訂單ID,項目說明等

** MULTILINE匹配基於訂單ID

PRIMARY發票

初級(發票)=訂單ID,姓名,地址,結算條件等

**主要是每單編號一行記錄

「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 

末次

+0

不要使用DoCmd.SetWarnings False,因爲您無法修復您忽略的錯誤(例如:miss在FROM和WHERE之前的空格,SELECT中的每個字符串必須用引號括起來)。使用'Debug.Print SQL'來檢查你的SQL。使用錯誤處理程序來處理錯誤。使用'db.Execute SQL,dbFailOnError'而不是'DoCmd.RunSQL'獲取更多錯誤信息。 – BitAccesser

回答

1

那是因爲你正在更新一個字符串,VBA不能在這樣的字符串中使用變量,這裏是doiing它的正確方法:

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

如果值是一個字符串,您必須添加引號:

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

不應該是'rs.Fields(「OrderID」)。Value'? – BitAccesser

+0

對,我看到第一件事,我沒有注意到:( –

+1

仍然是錯誤的屬性(.getvalue)。修復這個爲upvote。 – BitAccesser