Home » excel » sql – ADODB/Excel query error

sql – ADODB/Excel query error

Posted by: admin May 14, 2020 Leave a comment

Questions:

When I try to use ADODB to connect on an Excel DB, it says:

you try to execute a query without a specified expression <Destinataire>

Here’s my code:

MyQuery = "Select Destinataire, SUM(" & Entete & ") AS NombreTotal FROM [Feuil1$] " _
                         & "WHERE [DateMad] Between #" _
                         & Format(date_deb, "yyyy/mm/dd") _
                         & "# And #" & Format(date_fin, "yyyy/mm/dd") & "#" & Query3 & ""

                    objRecordSet.Open MyQuery, objConnection, adOpenStatic, adLockOptimistic

What’s wrong with it?

How to&Answers:

You forgot the group by

The query should look like this

Select Destinataire, SUM(NbCompteurElec) AS NombreTotal FROM [Feuil1$] 
WHERE [DateMad] Between #2012/10/22# And #2012/10/26# And [Destinataire] = 'REL12'
Group By Destinataire

In your code it should be like this

MyQuery = "Select Destinataire, SUM(" & Entete & ") AS NombreTotal FROM [Feuil1$] " _
                         & "WHERE [DateMad] Between #" _
                         & Format(date_deb, "yyyy/mm/dd") _
                         & "# And #" & Format(date_fin, "yyyy/mm/dd") & "#" & Query3 & " Group By Destinataire"

Here’s a tuto about the GROUP BY clause