简体   繁体   中英

SQL Syntax Issue MS Access VBA

I'm having problems with the syntax: I transferred this from SQL Design View to VBA. [area] is numeric. I'm simply asking you guys to do a cursory review and tell me if anything Jumps out at you.

DoCmd.RunSQL "SELECT 'VCAP0112' AS VCAP0112, VCAP0112.[RECV IND] AS OMU, VCAP0112.[LEGACY ACCT], Sum(VCAP0112.[1 to 30 Day]) AS [0 - 30], " _
        & "Sum(VCAP0112.[31 to 60 Day]) AS [31 - 60], Sum(VCAP0112.[61 to 90 Day]) AS [61 - 90], Sum(VCAP0112.[91 to 120 Day]) AS [91 -  120], " _ 
        & "Sum(VCAP0112.[Greater than 120]) AS [+120], Sum(VCAP0112.[Pmt Difference]) AS TOTAL " _
        & "FROM Urcrosswalk INNER JOIN VCAP0112 ON Urcrosswalk.[Legacy GL] = VCAP0112.[LEGACY ACCT] " _
        & "WHERE (((Left([VCAP0112].[area], 2))=80 Or (Left([VCAP0112].[area], 2))= 81)) " _
        & "GROUP BY 'VCAP0112', VCAP0112.[RECV IND], VCAP0112.[LEGACY ACCT] " _
        & "HAVING (((VCAP0112.[RECV IND])='O' Or (VCAP0112.[RECV IND])='M' Or (VCAP0112.[RECV IND])='U'));"

在此输入图像描述

You can't use DoCmd.RunSQL with a plain SELECT query.

From the DoCmd.RunSQL Method help topic ...

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. Include an IN clause if you want to access another database.

If your intention is to view the results from that query in Datasheet View, create a saved query with its SQL.

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSelect As String

strSelect = "SELECT ... blah, blah, blah"
Debug.Print strSelect '<- inspect this in Immediate window; Ctrl+g will take you there
Set db = CurrentDb
Set qdf = db.CreateQueryDef("YourQueryNameHere", strSelect)

Then you can open the saved query with DoCmd.OpenQuery ...

DoCmd.OpenQuery "YourQueryNameHere"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM