What's wrong with my SQL code?

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,294
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
How do I change the following SQL statement (I get an error on Sheet1$DA1)?

Code:
    strSQL = "INSERT INTO [" & strTablename & "] ([username]) " _
    & "SELECT F1 As username FROM " _
    & "[Excel 8.0;HDR=NO;DATABASE=" & ActiveWorkbook.FullName & "].[Sheet1$DA1]"
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
What's the error you get? Also you shouldn't be querying the active open workbook, it causes memory leaks
 
Upvote 0
The error is that the MS Access database engine can't find the object Sheet1$DA1. The data is supposed to go a MS Access table (strTablename).

The code only activates when the Workbook opens.
 
Upvote 0
Code:
strSQL = "INSERT INTO [" & strTablename & "] ([username]) " _
    & "SELECT F1 As username FROM " _
    & "[Excel 8.0;HDR=NO;DATABASE=" & ActiveWorkbook.FullName & "].[Sheet1$DA1:DA1]"

seems to work. Hmmmm.
 
Upvote 0
Glad you got it sorted, but ActiveWorkbook.FullName means you are querying the active workbook - this causes memory leaks in conjunction with ado
 
Upvote 0
Basically the Active Workbook can only be used by 1 person at a time. So what I was trying to do was that when the workbook opened it transferred the username to Access db which needs to append data to the workbook. Once the code has run, then that's it.

If there is another way of accomplishing this task without memory leaks, I would be interested.
 
Upvote 0
Something like (access syntax isn't my thing, so it might need tweaking):
Code:
strSQL = "INSERT INTO [" & strTablename & "] ([username]) Values ('" & ActiveWorkbook.Sheet1.Range("DA1").value &"');"
 
Upvote 0
I get error 438 - Object doesn't support this property or method.
 
Upvote 0
typo ;)
Code:
strSQL = "INSERT INTO [" & strTablename & "] ([username]) Values ('" & activeworkbook.Sheets("sheet1").Range("DA1").value &"');"
 
Upvote 0
That did it. Many thanks for your perseverence.
 
Upvote 0

Forum statistics

Threads
1,203,540
Messages
6,055,999
Members
444,839
Latest member
laurajames

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