【发布时间】:2016-10-25 21:52:57
【问题描述】:
背景
开发一个 MS Access 模块,在 onClick() 事件触发时执行 SQL 代码。
代码
Dim sqlString As String
sqlString = "SELECT [Table - Summary - All Item Forecasts, Sales, and POs].[COMPANY ID], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[COMPANY NAME], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].ITEM, " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].STYLE, " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].DESCRIPTION, " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[SALE PRICE], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[ON-HAND QTY], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[FORECAST QTY], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[MaxOfLATEST PO DATE] " _
& "FROM [Table - Active Product Catalog] " _
& "LEFT JOIN [Table - Summary - All Item Forecasts, Sales, and POs] " _
& "ON [Table - Active Product Catalog].STYLE = [Table - Summary - All Item Forecasts, Sales, and POs].STYLE " _
& "GROUP BY [Table - Summary - All Item Forecasts, Sales, and POs].[COMPANY ID], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[COMPANY NAME] , " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].Item, " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].Style, " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].Description, " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[SALE PRICE], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[ON-HAND QTY], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[FORECAST QTY], " _
& "[Table - Summary - All Item Forecasts, Sales, and POs].[MaxOfLATEST PO DATE]" _
& "HAVING ((([Table - Summary - All Item Forecasts, Sales, and POs].[COMPANY NAME]) Like ' * ' & [Enter Company Name:] & ' * '));"
DoCmd.RunSQL sqlString
错误
运行时错误“2342”:RunSQL 操作需要一个由 SQL 语句组成的参数。
问题
- 我已按照以下tutorial 操作,但收到错误消息。 RunSQL 命令似乎没有将 sqlString 识别为 SQL 命令。但是,我很肯定 SQL 语句在 MS Access 中正确运行。有什么想法吗?
- 在 SQL 语句的最后一行,我有一个 MsgBox 提示符(“
[Enter Company Name:]”)。我不太确定这是否会影响 SQL 语句,但如前所述,该语法在 MS Access 中运行;即使我用硬编码值替换提示,我也会收到相同的错误消息。
注意:我很可能会创建一个 inputBox 来请求 company name,然后将变量放在 SQL 语句的最后一行。
【问题讨论】:
-
考虑在你的 SQL (
SELECT Summary.[COMPANY ID] FROM [Table - Summary - All Item Forecasts, Sales, and POs] AS Summary) 中使用表别名;它会让你的语句更短更容易解析。