使用VBA将excel表数据导出到Access DB - “INSERT INTO”的无尽问题

时间:2018-06-18 10:27:02

标签: excel vba excel-vba ms-access

我的同事使用Excel跟踪器来跟踪他们正在解析的票证,我想实现一个系统,他们可以将他们的跟踪器上传到存储在共享文件夹中的中央访问数据库。 我有宏设置,它工作正常一次或两次,但他们似乎无缘无故地停止使用错误消息:

运行时错误'-2147217900(80040e14)': INSERT INTO语句包含以下未知字段名称:'F13'。确保您已正确输入名称,然后再次尝试操作。

我已经一次又一次地验证我的excel表中的所有字段都与我的Access数据库中的字段匹配并拼写正确(事实上我甚至没有字段名称'F13' excel表或DB,如果我引入一个来满足错误消息它反而说它有一个未知的字段名称'F14',依此类推)

我不知道这个幽灵领域来自它正在寻找的地方。

这是我的代码:

    Public Sub UploadToAccessDB()

'Skip operation if table contains no data
If ActiveSheet.ListObjects(1).ListRows.Count = 1 Then
    MsgBox ("There is no data to upload")
    GoTo Skip
    Else
    End If


'Import tracker table rows to access
  Set cn = CreateObject("ADODB.Connection")
  dbPath = " Redacted to protect the innocent :-P "                 'filepath of target access DB
  dbWb = Application.ActiveWorkbook.FullName
  dbWs = Application.ActiveSheet.Name
  scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
  dsh = "[" & Application.ActiveSheet.Name & "$]"
  cn.Open scn


  ssql = "INSERT INTO Tracker ([Ticket URL], [Item / Reason], [Date Created], [Date Resolved / HandOff], [Handed Off to], [Keeper's Login], [Category], [Site], [Processing Time], [Tracker Upload Date], [Uploaded By], [Team] ) "      'Field names from tracker
  ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh


  cn.Execute ssql            ' < Where the error is occurring


MsgBox ("Tracker uploaded to database")


Skip:

End Sub

我认为这是一个访问错误,是否有人知道任何解决方法或其他方法可以解决此问题?

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:0)

我建议您在前面Nathan_Sav和Darren Bartrup-Cook的评论中提到的方法存在许多问题。

  1. 首先,我会编写一些代码,打开并清除电子表格中的最差错误(日期格式不是日期,额外的列等,其中包含空格的列名称)。 例如这是一个修复大多数日期的例程(格式化为文本的日期除外)
  2. `

     Function FixDateInCell(DataCell As Excel.Range, CellOffset As Long)
    
      Dim tmpDate As Variant
      Dim TheDatecell As Excel.Range
    
     Set TheDatecell = DataCell.Offset(0, CellOffset)
    
     'Date of Last Inspection....
      If IsPrettyVacant(TheDatecell.Value) Then 'if there is a date
          TheDatecell.Value = Null 'blanks and spaces can result in errors during import
      ElseIf (TheDatecell.Value) = 0 Or (TheDatecell.Value) = 1 Then
          TheDatecell.Value = Null ''blanks can result in errors during import
    
      ElseIf (IsDate(TheDatecell.Value) = True) Then 'this returns TRUE for dates as text
          tmpDate = TheDatecell.Value
          If TypeName(tmpDate) = "Date" Then
            'do nothing it's a true date, we don't need to fix it
          Else
            TheDatecell.Value = DateValue(TheDatecell.Value) 'fix it
          End If
      ElseIf Excel.Application.WorksheetFunction.IsText(TheDatecell.Value) Then
          On Error Resume Next
          If IsError(DateValue(TheDatecell.Value)) Then GoTo CantFix:
           'Don't attempt to convert it to a date
           'MsgBox "DateValue returns:" & DateValue(TheDatecell.Value), vbOKOnly
           'newdate() = Split(TheDatecell.Value, "/")
    
      End If
    CantFix:
    
    End Function`    
    
    1. 其次,我将电子表格导入到您之前创建的表格中,并将所有数据类型(日期除外)设置为“长文本”。然后尝试处理数据或将其导入到另一个表中(无法导入的东西将被遗忘)