将 Excel 文件导入 Access 表运行时错误 '-2147352565 (8002000b)'

发布于 2024-09-30 03:59:28 字数 1214 浏览 3 评论 0原文

场景:用户要求我向他们提供一个按钮,他们可以在其中选择 .xls,然后它将数据导入到表中的相应列中。

问题:我将提供下面的代码,但基本上一旦它尝试打开工作簿,就会出现以下错误。我已经用谷歌搜索了许多解决方案,但仍然收到此错误。 替代文本

    Private Sub Command20_Click()
     Dim fdg As FileDialog, vrtSelectedItem As Variant
Dim strSelectedFile As String

Set fdg = Application.FileDialog(msoFileDialogFilePicker)

With fdg
  .AllowMultiSelect = False
  .ButtonName = "Select"
        .InitialView = msoFileDialogViewList
        .Title = "Select Input Files"

        'add filter for excel
        With .Filters
            .Clear
            .Add "Excel Spreadsheets", "*.xls"
        End With
        .FilterIndex = 1
    If .Show = -1 Then
      For Each vrtSelectedItem In .SelectedItems    'onby be 1
      Dim app As New Excel.Application

      app.Workbooks.Open (vrtSelectedItem)
      app.Worksheets(1).Activate
      For Each rwRow In ActiveWorkbook.Worksheets(1).Rows
        ' Do things with the rwRow object
        Next rwRow
        strSelectedFile = vrtSelectedItem
      Next vrtSelectedItem
      Me![txtSelectedFile] = strSelectedFile
    Else     'The user pressed Cancel.
    End If
End With

Set fd = Nothing
End Sub

Scenario: A user has asked me to provide them a button where they can select a .xls and it will import the data into the corresponding columns in a table.

Question: I will provide the code below, but basically once it tries to open the workbook it gives me the error below. I have googled for a number of solutions but I'm still getting this error.
alt text

    Private Sub Command20_Click()
     Dim fdg As FileDialog, vrtSelectedItem As Variant
Dim strSelectedFile As String

Set fdg = Application.FileDialog(msoFileDialogFilePicker)

With fdg
  .AllowMultiSelect = False
  .ButtonName = "Select"
        .InitialView = msoFileDialogViewList
        .Title = "Select Input Files"

        'add filter for excel
        With .Filters
            .Clear
            .Add "Excel Spreadsheets", "*.xls"
        End With
        .FilterIndex = 1
    If .Show = -1 Then
      For Each vrtSelectedItem In .SelectedItems    'onby be 1
      Dim app As New Excel.Application

      app.Workbooks.Open (vrtSelectedItem)
      app.Worksheets(1).Activate
      For Each rwRow In ActiveWorkbook.Worksheets(1).Rows
        ' Do things with the rwRow object
        Next rwRow
        strSelectedFile = vrtSelectedItem
      Next vrtSelectedItem
      Me![txtSelectedFile] = strSelectedFile
    Else     'The user pressed Cancel.
    End If
End With

Set fd = Nothing
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的轨迹 2024-10-07 03:59:28

嗯..为我工作。

之后的四行

我注释掉了 app.Worksheets(1).ActivateMe![txtSelectedFile] = ... The set fd = Nothing code> 我不得不更改为 fdg

我想知道 Excel 文件本身是否有问题。您尝试过从头开始创建一个吗?

Hmmm.. works form me.

I commented out the four lines after app.Worksheets(1).Activate and the Me![txtSelectedFile] = ...

The set fd = Nothing I had to change to fdg

I wonder if something is wrong with the Excel file it self. have you tried one created from scratch?

傲性难收 2024-10-07 03:59:28

我不明白为什么 app.Workbooks.Open 会导致该错误。但是,我会建议一些更改,这些更改可能会也可能不会帮助您识别问题。

  1. Option Explicit 添加到模块的声明部分;然后从 VBE 主菜单运行“调试”->“编译”,以显示
  2. Dim app As New Excel.Application 之后您还没有 Dim 包含 app.Visible = True 的 使用 Excel 添加 app.QuitSet app = Nothing 来监控发生的情况
  3. 变量code> 以便您可以在程序结束之前

I don't understand why app.Workbooks.Open causes that error. However, I will suggest some changes which may or may not help you identify the problem.

  1. add Option Explicit to your module's Declarations section; then run Debug->Compile from the VBE main menu to show you which variables you haven't Dim'd
  2. include app.Visible = True after Dim app As New Excel.Application so you can monitor what's happening with Excel
  3. add app.Quit and Set app = Nothing before the end of your procedure
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文