Avatar of Peter Fors
Peter ForsFlag for Sweden

asked on 

From Access to Excel having Error -2147217900 (Invalid SQL statement...

I have code for Office Automation. Data in Access turns into a chart in Excel. The code works normally as expected. But now I receive an error:


Error -2147217900 (Invalid SQL statement; expected DELETE, INSERT, PROCEDURE, SELECT or UPDATE, line 230

The code (part of it) is like this:

          Dim conQuery As String
110       conQuery = "[ChartDataOGAEpH]"
            ' Create recordset.           ‘Debug.Print conQuery 220       Set rst = New ADODB.Recordset 230       rst.Open _             Source:=conQuery, _             ActiveConnection:=CurrentProject.Connection

Open in new window

Error when the SQL is:

SELECT Format([MätDatum],"Short Date") AS Registered, [Jäsprotokoll rader].OG, [Jäsprotokoll rader].SG AS AE, [Jäsprotokoll rader].pH

FROM Jäsprotokoll INNER JOIN [Jäsprotokoll rader] ON Jäsprotokoll.JäsprotokollID = [Jäsprotokoll rader].JäsprotokollID

WHERE (((Format([MätDatum],"Short Date")) Is Not Null) AND (([Jäsprotokoll rader].SG) Is Not Null) AND ((Jäsprotokoll.JäsprotokollID)=[Forms]![Jäsprotokoll historiskt]![JäsprotokollID]))

ORDER BY Format([MätDatum],"Short Date");

Although the output of the query is:

ChartDataOGAEpH

Registered

OG

AE

pH

2021-09-07

12,9

12,9

5,2

2021-09-08

12,9

9,5

4,6

2021-09-09

12,9

6

4,35

2021-09-10

12,9

3

4,3

2021-09-13

12,9

2,5

4,4

2021-09-14

12,9

2.5

4,41

2021-09-15

12,9

2,5

4,48


What am I doing wrong?

VBA* excel graphMicrosoft Access

Avatar of undefined
Last Comment
Peter Fors
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Have you checked the query via SSMS ?
If the connection is directly to SQL then you have check the commands ...e.g SQL doesn't have Format
Avatar of Peter Fors
Peter Fors
Flag of Sweden image

ASKER

Hi John,
Thanks for helping.
It is all Access, the underlying tables as well as the query.
But I removed the Format.
Still the same Error.

I then tried to change the query and random picked a table as source (conquery = "MyPickedTable")
Now the code works as expected.

I can workaround the issue via a Query - > Table.
But why is there an error at all?
So no SQL server ?
What about DAO recordset?
Dim rst as DAO.Recordset
Dim strSQL as String
strSQL = ' Your sql
set rst = CurrentDb.OpenRecordset(strSQL)
Debug.print rst.Recordcount  

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Peter Fors
Peter Fors
Flag of Sweden image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo