2016-07-04 106 views
0

我在表單設計中工作,我希望用戶從組合框中選擇購買項目並鍵入單價和購買數量。用戶選擇和鍵入的所有信息將被添加到另一個表中。這裏我使用Do Until Loop逐個添加購買記錄。我嘗試了Do Until Loop。它運作良好。然而,正如我結束,這與循環Insert Into語句我收到一個語法錯誤。請幫我檢查問題出在哪裏。謝謝。訪問中的SQL插入語句的語法錯誤

我的代碼:

Private Sub Command133_Click() 
Dim i As Integer 
i = 1 
Do Until IsNull(Me.Controls("cboPurchaseItem" & i)) 
    Dim StrSql As String 
    StrSql = "Insert Into test2 (PurchaseID, 
           PurchaseDate, 
           ExpectedDeliveryDate, 
           Supplier, 
           PurchaseItem, 
           Unit, 
           PurchaseQuantity, 
           UnitCost, 
           OrderStatus,)" & _ 
      " VALUES('" & Me!txtOrderID & "', 
        #" & Format(Me!txtOrderDate, "yyyy/mm/dd") & "#, 
        #" & Format(Me!txtDeliveryDate, "yyyy/mm/dd") & "#, 
        '" & Me!cboSupplierCompany & "', 
        '" & Me.Controls("cboPurchaseItem" & i) & "', 
        '" & Me.Controls("txtUnit" & i) & "', 
        " & CStr(Me.Controls("TxtQty" & i)) & ", 
        " & CStr(Me.Controls("TxtPrice" & i)) & ", 
        'Ordered')" 
    CurrentDb.Execute (StrSql) 
MsgBox " You have successfully added " & i & " records to the PurchaseOrderDetail table." 
i = i + 1 
Loop 
End Sub 
+0

我發現了一個錯誤。 OrderStatus後錯誤地輸入了一個逗號「,」。這是錯誤信息。令我驚訝的是,Me!cboSupplierCompany和Me.Controls(「cboPurchaseItem」&i)中的數據無法添加到表中空白處。 –

回答

0

考慮使用它避免了引號或數字括值需要QueryDef對象參數化的SQL查詢(在現代編程最佳實踐)。只需在字符串語句頂部添加PARAMETERS子句,然後在循環中迭代地綁定值。

至於在空白時失敗的組合框,可能是由於VBA nullstring無法在查詢中作爲字符串字面值傳遞。要解決此問題,請嘗試使用Nz()函數在組合框爲空時返回零​​長度的字符串值。

Dim qdef As Querydef 
Dim i As Integer: i = 1 
Dim StrSql As String 

' PREPARE SQL STATEMENT ' 
StrSql = "PARAMETERS [PIDParam] Integer, [PDateParam] Datetime," _ 
      & " [EDDateParam] Datetime, [SupplierParam] Text(255)," _ 
      & " [PItemParam] Text(255), [UnitParam] Text(255), [PQtyParam] Long," _ 
      & " [UnitCostParam] Double, [OrderStatusParam] Text(255);" _ 
      & " INSERT INTO test2 (PurchaseID, PurchaseDate," _ 
      & "     ExpectedDeliveryDate, Supplier, PurchaseItem, Unit," _ 
      & "     PurchaseQuantity, UnitCost, OrderStatus)" _ 
      & " VALUES ([PIDParam], [PDateParam], [EDDateParam]," _ 
      & "   [SupplierParam], [PItemParam], [UnitParam], [PQtyParam]," _ 
      & "   [UnitCostParam], [OrderStatus]);" 

Do Until IsNull(Me.Controls("cboPurchaseItem" & i)) 
    ' INITIALIZE QDEF ' 
    Set qdef = CurrentDb.CreateQyerydef("", StrSqL) 

    ' BIND PARAMETERS TO STATEMENT ' 
    qdef!PIDParam = Me!txtOrderID 
    qdef!PDateParam = Me!txtOrderDate 
    qdef!EDDatePram = Me!txtDeliveryDate 
    qdef!SuppierPram = Nz(Me!cboSupplierCompany) 
    qdef!PItemParam = Nz(Me.Controls("cboPurchaseItem" & i)) 
    qdef!UnitParam = Me.Controls("txtUnit" & i) 
    qdef!PQtyParam = CStr(Me.Controls("TxtQty" & i)) 
    qdef!UnitCostParam = CStr(Me.Controls("TxtPrice" & i)) 
    qdef!OrderStatusParam = "Ordered" 

    ' EXECUTE APPEND QUERY ' 
    qdef!Execute dbFailOnError 
    i = i + 1 
Loop 

MsgBox "You have successfully added " & i _ 
     & " records to the PurchaseOrderDetail table.", vbInformation, "APPEND SUCCESS" 

Set qdef = Nothing