MS EXCEL到MS ACCESS .accdb数据库来自VBA SQL语法错误

我完全被卡住了,把头发拉出来。

从Excel VBA我有两套代码:

1-创build一个表是MS Access通过SQL语句

2-使用For循环填充新创build的表,也使用SQL

第一组代码完美地工作,所以我知道我的连接string正常工作。

这是第一套:

Sub Create_Table() 'Add Reference to Microsoft ActiveX Data Objects 2.x Library Dim strConnectString As String Dim objConnection As ADODB.Connection Dim strDbPath As String Dim strTblName As String Dim wCL As Worksheet Dim wCD As Worksheet Set wCL = Worksheets("Contract List") Set wCD = Worksheets("Contract Data") 'Set database name and DB connection string-------- strDbPath = ThisWorkbook.Path & "\SpreadPrices.accdb" '================================================== strTblName = wCL.Range("TableName").Value strConnectString = "Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDbPath & ";" 'Connect Database; insert a new table Set objConnection = New ADODB.Connection On Error Resume Next With objConnection .Open strConnectString .Execute "CREATE TABLE " & strTblName & " (" & _ "[cDate] text(150), " & _ "[Open] text(150), " & _ "[High] text(150), " & _ "[Low] text(150), " & _ "[Last] text(150), " & _ "[cChange] text(150), " & _ "[Settle] text(150), " & _ "[cVolume] text(150), " & _ "[OpenInterest] text(150))" End With Set objConnection = Nothing End Sub 

之前提到的代码完美的工作。 该错误是在用于填充表的以下代码集。

这里是:

 Sub InsertSQL() 'Add Reference to Microsoft ActiveX Data Objects 2.x Library Dim strConnectString As String Dim objConnection As ADODB.Connection Dim strDbPath As String Dim strTblName As String Dim lngRow As Long Dim strSQL As String Dim wCL As Worksheet Dim wCD As Worksheet Set wCL = Worksheets("Contract List") Set wCD = Worksheets("Contract Data") 'Set database name and DB connection string-------- strDbPath = ThisWorkbook.Path & "\SpreadPrices.accdb" '================================================== strTblName = wCL.Range("TableName").Value strConnectString = "Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDbPath & ";" 'Connect Database; insert a new table Set objConnection = New ADODB.Connection 'On Error Resume Next With objConnection .Open strConnectString For lngRow = 2 To Range("NumberRows").Value strSQL = "INSERT INTO " & strTblName & " (" & _ "cDate, Open, High, Low, Last, cChange, Settle, cVolume, OpenInterest)" & _ " VALUES ('" & _ wCD.Cells(lngRow, 1) & "' , '" & _ wCD.Cells(lngRow, 2) & "' , '" & _ wCD.Cells(lngRow, 3) & "' , '" & _ wCD.Cells(lngRow, 4) & "' , '" & _ wCD.Cells(lngRow, 5) & "' , '" & _ wCD.Cells(lngRow, 6) & "' , '" & _ wCD.Cells(lngRow, 7) & "' , '" & _ wCD.Cells(lngRow, 8) & "' , '" & _ wCD.Cells(lngRow, 9) & "')" wCL.Range("A1").Value = strSQL .Execute strSQL Next lngRow End With Set objConnection = Nothing End Sub 

我收到的错误是:

运行时错误,INSERT INTO语句中的语法错误。

好吧,所以起初我想我的SQLstring中必须有一个错误。 所以,我把确切的SQLstring,并将其折腾到Access查询生成器,并运行SQL命令,它import到表中就好了。

我错过了什么?

问题可能是由于字段名称。 有一个名为CDate的函数。 OpenLast都是Jet保留字。 请参阅Access中的问题名称和保留字 。

将这些问题字段名称括在方括号中以避免混淆数据库引擎:

 "[cDate], [Open], High, Low, [Last], cChange, Settle, cVolume, OpenInterest)" 

括号可能足以让你的INSERT工作。 不过可以考虑重命名这些字段。

该链接页面还提到了Allen Browne的数据库问题检查工具 。 您可以下载该实用程序并使用它来检查数据库中的其他问题名称。 它还可以提醒您其他可能不会影响当前INSERT问题的问题,但可能会在其他情况下造成麻烦。