Problem using SQL string to import data from Access table

atf32

Board Regular
Joined
Apr 13, 2011
Messages
157
:confused:Currently I am having a problem with importing data from an access table using an SQL statement via VBA. The table is a simple one, with Column 1 (called FName) and holds a list of first names. Column 2 (called LName) and holds a list of of last names. My string is as follows:

MySQL = "SELECT LName, FName" & _
" FROM tblClientNames" & _
" GROUP BY LName"
With the above string, I get an error message (run-time error '-2147217887(80040e21) with the message "You tried to execute a query that does not include the specified expression 'FName' as part of an aggregate function"

If I exclude the "FName" column, the data is imported. However, I only get the column of last names (LName). :confused:

I cannot find a resolution on this...Need help on this
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Group By clauses are usually needed when using SQL aggregate functions (such as SUM, COUNT, MAX, MIN, AVG). The use of a group by clause is superfluous here.

Just:
Code:
MySQL = "SELECT LName, FName FROM tblClientNames;"
If you want only the unique last name, first names use DISTINCT:
Code:
MySQL = "SELECT DISTINCT LName, FName FROM tblClientNames;"
I honestly don't know if this would work but you could try (it's bad form, I think) if you really want to group the names:
Code:
MySQL = "SELECT LName, FName" & _
" FROM tblClientNames" & _
" GROUP BY LName, FName;"
 
Upvote 0

Forum statistics

Threads
1,224,267
Messages
6,177,549
Members
452,783
Latest member
back1ply

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top