使用下面的代码我找不到运行提供程序时出现以下错误,下面的代码是从网上复制和编辑的,它以前使用.mdb文件,但我试图将其更改为.accdb,因为那是格式我需要它。我正在尝试创建一个宏,当运行时将某些单元格复制到数据库中,然后添加它。
我收到此错误
run-time error "3706"
Provider cannot be found it may not be properly installed
-
Const TARGET_DB = "testdb.accdb"
Sub AlterOneRecord()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim lngRow As Long
Dim lngID As String
Dim j As Long
Dim sSQL As String
'determine the ID of the current record and define the SQL statement
lngRow = ActiveCell.Row
lngID = Cells(lngRow, 1).Value
sSQL = "SELECT * FROM tblPopulation WHERE PopID = " & lngID
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.path & Application.PathSeparator & TARGET_DB
With cnn
.Provider = "Provider=Microsoft.ACE.OLEDB.12.0;"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, _
ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, _
LockType:=adLockOptimistic
'Load contents of modified record from Excel to Access.
'do not load the ID again.
For j = 2 To 7
rst(Cells(1, j).Value) = Cells(lngRow, j).Value
Next j
rst.Update
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
End Sub
有更简单的方法吗?或者我应该尝试解决这个问题?
答案 0 :(得分:3)
您缺少的是与数据库文件的完整连接字符串。
(More about connection string)
我给你一个粗略的想法,通常与我的代码一起使用:
在代码中删除此行:
<击> .Provider = "Provider=Microsoft.ACE.OLEDB.12.0;"
击>
改为使用这个:
.ConnectionString= "Provider=Microsoft.ACE.OLEDB.12.0;"
或者你可以改用这个:
.Provider = "Microsoft.ACE.OLEDB.12.0"
有关详细信息,请参阅this w3schools website。