BACKGROUND
Develop a MS Access module that executes SQL code when onClick() event triggered.
CODE
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
ERROR
Run-time error '2342': A RunSQL action requires an argument consisting of an SQL statement.
QUESTION(S)
[Enter Company Name:]
"). I am not quite sure if this is throwing off the SQL statement but, as stated before, the syntax runs in MS Access; even if I replace the prompt with a hard-coded value, I receive the same error message. NOTE: I'll most likely create an inputBox
that asks for the company name
and then place the variable within the last line of the SQL statement.
As mentioned in the DoCmd.RunSQL documentation , it is used to run an "action query". That is, the SQLStatement is
A string expression that's a valid SQL statement for an action query or a data-definition query. It uses an INSERT INTO, DELETE, SELECT...INTO, UPDATE, CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, or DROP INDEX statement.
Note that the above list includes SELECT...INTO
but not a plain SELECT
. To execute a plain SELECT
and return a recordset you need to do something like
Dim cdb As DAO.Database
Set cdb = CurrentDb
Dim rst As DAO.Recordset
Set rst = cdb.OpenRecordset(sqlString, dbOpenSnapshot)
' loop through the Recordset contents
Also, in your last 2 lines you're missing the space between ...[MaxOfLATEST PO DATE] and HAVING. It will fall over when it tries to run that as SQL.
I've hit this myself a few times, as I'm very new to VBA - I always put a debug.print SQLstring after the run command as it's easier to find these missing spaces etc in the Immediate window.
HTH, Jenny
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.