使用 ADO 从 Excel 导入到 Access 时出错
Error importing from Excel to Access with ADO
我正在尝试使用 vba 将数据从 excel 文件导出到访问文件。
我的密码是
Sub Export_Data()
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rst As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath
Dim x As Long, i As Long
Dim nextrow As Long
'add error handling
On Error GoTo errHandler:
'Variables for file path and last row of data
dbPath = Sheet19.Range("I3").Value
nextrow = Cells(Rows.Count, 1).End(xlUp).Row
'Initialise the collection class variable
Set cnn = New ADODB.Connection
'Check for data
If Sheet18.Range("A2").Value = "" Then
MsgBox " Add the data that you want to send to MS Access"
Exit Sub
End If
'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
'two primary providers used in ADO SQLOLEDB —-Microsoft.JET.OLEDB.4.0 —-Microsoft.ACE.OLEDB.12.0
'OLE stands for Object Linking and Embedding, Database
'ADO library is equipped with a class named Recordset
Set rst = New ADODB.Recordset 'assign memory to the recordset
'ConnectionString Open '—-5 aguments—-
'Source, ActiveConnection, CursorType, LockType, Options
rst.Open Source:="ARF Form Log", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdTable
'you now have the recordset object
'add the values to it
For x = 2 To nextrow
rst.AddNew
For i = 1 To 29
rst(Cells(1, i).Value) = Cells(x, i).Value
Next i
rst.Update
Next x
'close the recordset
rst.Close
' Close the connection
cnn.Close
'clear memory
Set rst = Nothing
Set cnn = Nothing
'communicate with the user
MsgBox " The data has been successfully sent to the access database"
'Update the sheet
Application.ScreenUpdating = True
'show the next ID
Sheet19.Range("h7").Value = Sheet19.Range("h8").Value + 1
'Clear the data
Sheet18.Range("A2:ac1000").ClearContents
On Error GoTo 0
Exit Sub
errHandler:
'clear memory
Set rst = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data"
End Sub}
其中:
Sheet 19 包含我访问文件的路径
Sheet18个包含我要传输的数据
当我尝试将数据从 sheet 18 传输到访问权限 table 时,我收到错误消息:
过程 Export_Data
中的错误 -2147217900(From 子句中的语法错误。)
我已检查访问文件和 excel 文件的标题是否匹配,数据类型格式是否相同。
我不确定哪里有问题。
让我知道您的想法,感谢观看!
adCmdOpenTable
仍然需要将您的 table 名称括起来。本质上,它只是在源代码的开头附加 SELECT * FROM
,然后尝试执行它。因为您的 table 名称有空格,所以会导致 From
子句中出现语法错误。
添加括号或改用普通查询:
rst.Open Source:="SELECT * FROM [ARF Form Log]", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic
我正在尝试使用 vba 将数据从 excel 文件导出到访问文件。
我的密码是
Sub Export_Data()
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rst As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath
Dim x As Long, i As Long
Dim nextrow As Long
'add error handling
On Error GoTo errHandler:
'Variables for file path and last row of data
dbPath = Sheet19.Range("I3").Value
nextrow = Cells(Rows.Count, 1).End(xlUp).Row
'Initialise the collection class variable
Set cnn = New ADODB.Connection
'Check for data
If Sheet18.Range("A2").Value = "" Then
MsgBox " Add the data that you want to send to MS Access"
Exit Sub
End If
'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
'two primary providers used in ADO SQLOLEDB —-Microsoft.JET.OLEDB.4.0 —-Microsoft.ACE.OLEDB.12.0
'OLE stands for Object Linking and Embedding, Database
'ADO library is equipped with a class named Recordset
Set rst = New ADODB.Recordset 'assign memory to the recordset
'ConnectionString Open '—-5 aguments—-
'Source, ActiveConnection, CursorType, LockType, Options
rst.Open Source:="ARF Form Log", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdTable
'you now have the recordset object
'add the values to it
For x = 2 To nextrow
rst.AddNew
For i = 1 To 29
rst(Cells(1, i).Value) = Cells(x, i).Value
Next i
rst.Update
Next x
'close the recordset
rst.Close
' Close the connection
cnn.Close
'clear memory
Set rst = Nothing
Set cnn = Nothing
'communicate with the user
MsgBox " The data has been successfully sent to the access database"
'Update the sheet
Application.ScreenUpdating = True
'show the next ID
Sheet19.Range("h7").Value = Sheet19.Range("h8").Value + 1
'Clear the data
Sheet18.Range("A2:ac1000").ClearContents
On Error GoTo 0
Exit Sub
errHandler:
'clear memory
Set rst = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data"
End Sub}
其中:
Sheet 19 包含我访问文件的路径
Sheet18个包含我要传输的数据
当我尝试将数据从 sheet 18 传输到访问权限 table 时,我收到错误消息:
过程 Export_Data
中的错误 -2147217900(From 子句中的语法错误。)我已检查访问文件和 excel 文件的标题是否匹配,数据类型格式是否相同。
我不确定哪里有问题。
让我知道您的想法,感谢观看!
adCmdOpenTable
仍然需要将您的 table 名称括起来。本质上,它只是在源代码的开头附加 SELECT * FROM
,然后尝试执行它。因为您的 table 名称有空格,所以会导致 From
子句中出现语法错误。
添加括号或改用普通查询:
rst.Open Source:="SELECT * FROM [ARF Form Log]", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic