简体   繁体   中英

Using VBA to access an Access Database in Excel to transfer bulk data from a spreadsheet - error in query

I can't seem to figure out what is wrong with the from clause in the following statement:

INSERT INTO [tblFYPNameStudents] ([FypID], [Title], [StudentName]) SELECT * FROM [Excel 12.0;HDR=YES;Data Source='C:\Users\Lenovo\Documents\testwb.xlsm'].Sheet1$A5:C26

It results in 'Error 80040e14, syntax error in From Clause'

The code is as follows : (I'm new to VBA and all this but i couldn't figure out what is wrong. I think it has to do with the bit in the from clause where I specify excel and the data source)

Sub AccImport(tblName As String, _
    hasf As Boolean, _
    tblrange As String)
Set cn = CreateObject("ADODB.Connection")
sDB_Path = ActiveWorkbook.Path & Application.PathSeparator & TARGET_DB
dbWb = Application.ActiveWorkbook.FullName
dbWs = Application.ActiveSheet.Name
scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sDB_Path
strFile = Workbooks(1).FullName
dsh = tblrange
cn.Open scn
ssql = "INSERT INTO " & tblName
MsgBox ssql
ssql = ssql & "SELECT * FROM [Excel 12.0;HDR=YES;Data Source='" & dbWb & "']." & dsh
MsgBox ssql
cn.Execute ssql
End Sub

您的语法不太正确-应该显示为:

ssql = ssql & "SELECT * FROM [Excel 12.0;HDR=YES;Database=" & dbWb & "].[" & dsh & "]"

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