vba ADODB 连接到 ACCDB 文件——无法识别的数据库格式错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3321449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:55:44  来源:igfitidea点击:

ADODB connection to ACCDB file -- unrecognized database format error

sqlms-accessvba

提问by l--''''''---------''''''''''''

I have a file like this:

我有一个这样的文件:

strPath = "S:\HR\Forms\forms_database.accdb"

I am connecting to it through an WORD adodb.connection

我正在通过 WORD 连接到它 adodb.connection

should my database be a different extension ? MDB or something?

我的数据库应该是不同的扩展名吗?MDB什么的?

I am getting this error when it tries to connect:

尝试连接时出现此错误:

unrecognized database format 's:...............accdb'

what's going on here?

这里发生了什么?

here's the entire code:

这是整个代码:

Sub TransferShipper()

  'Transfer new shipping company record to

  'Shippers table in Northwind database.

  Dim cnn As ADODB.Connection
  Dim strConnection As String
  Dim strSQL As String
  Dim strPath As String
  Dim doc As Word.Document
  Dim strCompanyName As String
  Dim strPhone As String
  Dim bytContinue As Byte
  Dim lngSuccess As Long
  Set doc = ThisDocument
  On Error GoTo ErrHandler

  strCompanyName = Chr(39) & doc.FormFields("txtCompanyName").Result & Chr(39)
  strPhone = Chr(39) & doc.FormFields("txtPhone").Result & Chr(39)
  'Confirm new record.
  bytContinue = MsgBox("Do you want to insert this record?", vbYesNo, "Add Record")
  Debug.Print bytContinue
  'Process input values.
  If bytContinue = vbYes Then
    strSQL = "INSERT INTO vacation " _
     & "(title, department) " _
     & "VALUES (" _
     & strCompanyName & ", " _
     & strPhone & ")"
    Debug.Print strSQL
    'Substitute path and connection string with DSN if available.
    strPath = "S:\HR\Forms\forms_database.accdb"
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" _
     & "Data Source = " & strPath
    Debug.Print strConnection
    Set cnn = New ADODB.Connection
    cnn.Open strConnection
    cnn.Execute strSQL, lngSuccess
    cnn.Close
    MsgBox "You inserted " & lngSuccess & " record", _
     vbOKOnly, "Error Added"
    doc.FormFields("txtCompanyName").TextInput.Clear
    doc.FormFields("txtPhone").TextInput.Clear
  End If
  Set doc = Nothing
  Set cnn = Nothing
  Exit Sub
ErrHandler:
  MsgBox Err.Number & ": " & Err.Description, _
   vbOKOnly, "Error"
  On Error GoTo 0
  On Error Resume Next
  cnn.Close
  Set doc = Nothing
  Set cnn = Nothing
End Sub

回答by Tobiasopdenbrouw

Try ACE OLEDB 12.0 as the db version.

尝试 ACE OLEDB 12.0 作为数据库版本。

"Provider=Microsoft.ACE.OLEDB.12.0"