【问题标题】:Unrecognized DB Format-Excel VBA to Access Database无法识别的数据库格式-Excel VBA 访问数据库
【发布时间】:2015-07-08 13:00:48
【问题描述】:

我正在使用 .accdb 文件并连接以下代码,这对我来说已经工作了多次,所以我不知道为什么这次它会破坏文件。

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

“无法识别的格式”访问错误是否仅由于连接字符串中的错误而发生,还是我的 SQL 语句也插入了记录?谢谢



这是我的代码,如果有人愿意看一下。在构建 SQL statemetn(rcdDetail 变量)的 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:

【问题讨论】:

  • 您是否确认 Access 数据库本身没有问题 - 即数据库中没有损坏?如果 Access 出现了,嗯,打嗝,我不会感到震惊。
  • 哈哈是的,我几乎 100% 确定它的代码,因为我现在已经删除并重新创建了大约 5 次表。
  • 这个错误实际发生在什么时候?
  • Rory 每当代码完成时就会出现这种情况,我尝试打开数据库查看它
  • 为什么在CommitTrans 之前有OERN,为什么在插入查询中使用Recordset 而不是Connection.Execute

标签: vba excel ms-access


【解决方案1】:

一切看起来都不错,这就是我要尝试的。

手动输入 dbPath 的路径,以及整个连接字符串。可能路径未正确填充。

这是您可以遵循的连接字符串:

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

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

tblName = "[Wait_Data_Table]"

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多