查询语法错误...

时间:2012-03-01 10:25:17

标签: sql ms-access vba

我有一个按钮编码。我的问题是查询“SQLStory”出现错误,它缺少一个分号。

组合框包含项目名称,并按产品ID排序,SQLStory应将所有项目从TblTotalSale移动到表格TblSaleStore。任何错误的想法?

Private Sub StockOK_Click()
Dim SQLDelete1 As String
Dim SQLDelete2 As String
Dim SQLUpdate As String
Dim SQLStory As String

SQLDelete1 = "DELETE * FROM TblStock WHERE TblStock.ProductID = " & CboStockItem.Value
SQLDelete2 = "DELETE * FROM TblTotalSale WHERE TblTotalSale.ProductID = " & CboStockItem.Value
SQLUpdate = "INSERT INTO TblStock (ProductID, StockLevel) VALUES ( " & Me.CboStockItem.Value & "," & Me.TxtStockValue & " )"
SQLStory = "INSERT INTO TblSaleStore (ProductID) VALUES (TblTotalSale.ProductID) FROM TblTotalSale WHERE TblTotalSale.ProductID = " & Me.CboStockItem.Value


If IsNull(Me.TxtStockValue) Then MsgBox "Please Select An Item To Update Stock And Ensure A Value Has Been Entered" Else:
DoCmd.RunSQL SQLDelete1
DoCmd.SetWarnings False
DoCmd.RunSQL SQLStory
DoCmd.RunSQL SQLDelete2
DoCmd.RunSQL SQLUpdate
DoCmd.SetWarnings True


End Sub

我对这段代码的另一个问题是doCmd的块是否发生txt盒TxtStockValue是否为null,我只希望它们发生,如果盒子不为null ...那部分的任何想法要么?

由于

萨姆

1 个答案:

答案 0 :(得分:2)

值就是这样,诸如'abc'或123之类的值,你需要SELECT:

SQLStory = "INSERT INTO TblSaleStore (ProductID) " _
         & "SELECT (TblTotalSale.ProductID) FROM " _
         & "TblTotalSale WHERE TblTotalSale.ProductID = " _
         & Me.CboStockItem.Value

但上面的内容很奇怪,因为你已经在组合中有了ID,所以,正如我在你之前关于这个主题的文章中所说:

SQLStory = "INSERT INTO TblSaleStore (ProductID) " _
         & "VALUES ( " &  Me.CboStockItem.Value & " )"

另外,有人建议您在使用SQL时应该使用debug.print,这样您就可以查看SQL并将其粘贴到查询设计窗口中以查看它是否有效。当一切正常时,可以注释掉debug.print行。当您不熟悉SQL时,使用查询设计窗口构建查询有很多要说的。然后,您可以从SQL视图中删除SQL并添加引号等。

编辑问题第2部分

Dim db As Database
Set db = CurrentDB

If IsNull(Me.TxtStockValue) Then 
    MsgBox "Please Select An Item To Update Stock " _
           & "And Ensure A Value Has Been Entered" 
Else
   db.Execute SQLDelete1, dbFailOnError
   ''DoCmd.SetWarnings False
   db.Execute SQLStory, dbFailOnError
   db.Execute SQLDelete2, dbFailOnError
   db.Execute SQLUpdate, dbFailOnError
   ''DoCmd.SetWarnings True
End If