访问中更新sql的语法错误

时间:2016-09-18 16:03:47

标签: sql ms-access

我在访问2013中运行sql更新时出现语法错误。目标表保存了库存余额数据。要更新余额,原始数据将减去生产活动产生的消耗量。我使用查询来获取消费总和。我的代码如下:

Private Sub Command4_Click()
CurrentDb.Execute ("UPDATE tbl_Current_Stock As o" & _
                  “Inner join preview_Of_Raw_Material_Consumption As p” & _
                  “On o.Raw_Material =p.[Ingredient/Packaging material]” & _
                 "Set o.Stock_Level = o.Stock_Level- p.SumOfConsumption" )
 End Sub

编辑 - 添加无效的当前代码

CurrentDb.Execute ("UPDATE tbl_Current_Stock As o Inner join preview_Of_Raw_Material_Consumption As p On o.[Raw_Material] = p.[Ingredient/Packaging material]Set o.[Stock_Level] = o.[Stock_Level]- p.SumOfConsumption from o p")

2 个答案:

答案 0 :(得分:0)

您的查询如下所示:

UPDATE tbl_Current_Stock As oInner join preview_Of_Raw_Material_Consumption As pOn o.Raw_Material =p.[Ingredient/Packaging material]Set o.Stock_Level = o.Stock_Level- p.SumOfConsumption

你能发现问题吗?是的,您在查询中有运行行。如果在运行之前打印查询字符串,则问题很明显。

解决方案是在字符串中放置适当的空格:

CurrentDb.Execute ("UPDATE tbl_Current_Stock As o" & _
                   " Inner join preview_Of_Raw_Material_Consumption As p” & _
                   " On o.Raw_Material = p.[Ingredient/Packaging material]” & _
                   " Set o.Stock_Level = o.Stock_Level- p.SumOfConsumption" )

答案 1 :(得分:0)

谢谢戈登。我最终发现了我的代码的真正问题。我想分享我的经验,对于那些被同样或类似问题困扰的人来说,这可能会有所帮助。正如我在第一篇问题帖子中提到的,我的一个更新源来自一个查询。这个查询是个人消费的总和,它是“不可更新的”#34;。通过在线搜索,我使用时态表作为数据源,代码运行顺畅。

CurrentDb.Execute ("Update tbl_Current_Stock As o Inner join tbl_Temp_Raw_Material_Consumption As s On o.Raw_Material = s.[Ingredient/Packaging material] Set o.Stock_Level = o.Stock_Level - s.Consumption")

这里tbl_Temp_Raw_Material_Consumption是原始查询的生成表查询。