【发布时间】:2016-04-18 10:38:52
【问题描述】:
经过一番搜索,我找到了一个宏,可以将 Excel 工作表导出为受密码保护的 Access 数据库中的记录
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rst As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath
Dim i As Long
'add error handling
On Error GoTo errHandler:
'Variables for file path and last row of data
dbPath = ("\\serverpath\reporting.accdb")
'Initialise the collection class variable
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath, , "password"
Set rst = New ADODB.Recordset 'assign memory to the recordset
rst.Open Source:="table", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdTable
rst.AddNew
For i = 1 To 180
rst(Cells(1, i).Value) = Cells(nextrow, i).Value
Next i
rst.Update
'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
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"
我在cnn.Open 收到错误“错误 2147217843 工作组信息文件丢失”
【问题讨论】:
-
看this
标签: excel ms-access macros vba