无法识别的数据库格式 – Excel VBA访问数据库

我使用一个.accdb文件,并连接下面的代码,这已经多次为我工作,所以我不知道为什么这个时候会损坏文件。

dbPath = ActiveWorkbook.Path & "\WaitAnalysisDB.accdb" tblName = "Wait_Data_Table" strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & dbPath & "';" conn.Open strcon 

“无法识别的格式”访问错误只是由于连接string中的错误,或者是我的SQL语句插入logging? 谢谢

这是我的代码,如果有人在意通过它。 在构buildSQL statemetn(rcdDetailvariables)的for循环中,我有一个if语句,基本上说如果列A中有一个空白,则使用非空的上面的行。

 Dim conn As New ADODB.Connection, rs As New ADODB.Recordset, dbPath As String, tblName As String Dim rngColHeads As Range, rngTblRcds As Range, colHead As String, rcdDetail As String Dim ch As Integer, cl As Integer, notNull As Boolean, strcon As String, lr As Integer Dim currentdate As String Dim strdbcheck As String 'Code Checks if There Are Records for the Date in the DB 'If there is, then it skips the SQL code currentdate = Date dbPath = ActiveWorkbook.Path & "\WaitAnalysisDB.accdb" tblName = "Wait_Data_Table" strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & dbPath & "';" conn.Open strcon strdbcheck = "SELECT * FROM " & tblName rs.Open strdbcheck, conn rs.Filter = "Date= #" & currentdate & "#" If Not rs.EOF Then Set rs = Nothing Set conn = Nothing GoTo SkipExport Else Set rs = Nothing Set conn = Nothing GoTo Export End If Export: 'Set Up Connections dbPath = ActiveWorkbook.Path & "\WaitAnalysisDB.accdb" tblName = "Wait_Data_Table" 'Create Date Column Worksheets("Wait Analysis DATA").Select lr = Cells(Rows.Count, "K").End(xlUp).Row currentdate = Date: Range("O1").Value = "Date": Range(Range("O2"), Range("O" & lr)).Value = currentdate Set rngColHeads = ActiveSheet.Range(Range("a1"), Range("a1").End(xlToRight)) Set rngTblRcds = ActiveSheet.Range(Range("K2:k" & lr).Offset(0, -10), Range("K2:k" & lr).Offset(0, 4)) 'SQL connection String strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & dbPath & "';" 'Create String for Columns for SQL colHead = "([" For ch = 1 To rngColHeads.Count colHead = colHead & rngColHeads.Columns(ch).Value Select Case ch Case Is = rngColHeads.Count colHead = colHead & "])" Case Else colHead = colHead & "],[" End Select Next ch On Error GoTo EndUpdate conn.Open strcon conn.BeginTrans Dim tempcl As Integer For cl = 1 To rngTblRcds.Rows.Count If Range("a2").Offset(cl - 1, 0) = "" Then tempcl = cl - Range("a2").Offset(cl, 0).End(xlUp).Rows.Count notNull = False rcdDetail = "('" For ch = 1 To rngColHeads.Count Select Case rngTblRcds.Rows(tempcl).Columns(ch).Value Case Is = Empty Select Case ch Case Is = rngColHeads.Count rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL)" Case Else rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL,'" End Select Case Else notNull = True Select Case ch Case "11": rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "','" Case Is = rngColHeads.Count rcdDetail = rcdDetail & rngTblRcds.Rows(tempcl).Columns(ch).Value & "')" Case Else rcdDetail = rcdDetail & rngTblRcds.Rows(tempcl).Columns(ch).Value & "','" End Select End Select Next ch tempcl = 0 GoTo skipads End If notNull = False rcdDetail = "('" For ch = 1 To rngColHeads.Count Select Case rngTblRcds.Rows(cl).Columns(ch).Value Case Is = Empty Select Case ch Case Is = rngColHeads.Count rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL)" Case Else rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL,'" End Select Case Else notNull = True Select Case ch Case Is = rngColHeads.Count rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "')" Case Else rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "','" End Select End Select Next ch skipads: Select Case notNull Case Is = True rs.Open "INSERT INTO " & tblName & colHead & " VALUES " & rcdDetail, conn Case Is = False 'do not insert record End Select Next cl EndUpdate: If Err.Number <> 0 Then On Error Resume Next conn.RollbackTrans MsgBox "There was an error. This will exit the macro.", vbCritical, "Error!" End Else On Error Resume Next conn.CommitTrans End If conn.Close Set rs = Nothing Set conn = Nothing On Error GoTo 0 SkipExport: 

一切看起来不错,这是我想要的。

手动键入dbPath的path,而且整个连接string。 也许path没有正确填充。

以下是您可以遵循的连接string:

 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False; 

如果这不起作用,请仔细检查您的表名是否正确命名。 如果检出,请尝试在[]中包装您的表名。 所以:

 tblName = "[Wait_Data_Table]" 

这些是常见的事情,让我感到悲伤,也许其中之一也发生在你身上。