我完全被卡住了,拔了一根头发。
从Excel VBA,我有两套代码:
1-要创建表是通过SQL语句的MS Access
2-也使用SQL使用For循环填充新创建的表
第一组代码可以完美运行,因此我知道我的连接字符串可以正常工作。
这是第一组:
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语句中的语法错误。
好的,所以一开始我以为我认为我的SQL字符串一定有错误。因此,我将使用确切的SQL字符串并将其扔到Access Query Builder中并运行SQL命令,然后将其导入表中就可以了。
我想念什么?
该问题可能是由于字段名称引起的。有一个名为的函数CDate
。Open
并且Last
都是Jet保留字。请参阅Access中的问题名称和保留字。
将这些问题字段名称括在方括号中,以避免混淆数据库引擎:
"[cDate], [Open], High, Low, [Last], cChange, Settle, cVolume, OpenInterest)"
括号可能足以使您INSERT
正常工作。但是,如果可能,请考虑重命名字段。
该链接页面还提到了Allen Browne的Database Issue Checker Utility。您可以下载该实用程序,并使用它来检查数据库中的其他问题名称。它还可以警告您其他问题,这些问题可能不会影响当前INSERT
问题,但是在其他情况下可能会引起麻烦。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句