繁体   English   中英

通过VBA将MS Excel工作表导入到MS Access中,而不是全部导入

[英]Importing MS Excel sheet into MS Access via VBA not importing everything

我有一个MS Access数据库程序,该程序从MS Excel工作簿(仅一张工作表)中引入数据,然后对该数据进行处理并为用户列出完整的表格。

整个过程运行良好,但是当他们在报告中遇到错误时,我去使用数据库程序来查找问题,然后又出现了一个新问题。 MS Access仅导入单元格A1和A2。

当我尝试手动导入Excel文件并追加到表时,导入向导仅将这两个单元格显示为包含在文件中。 不知何故,MS Access无法看到整个数据表。

我尝试过手动更改DoCmd.TransferSpreadsheet中的acSpreadsheetType选项,它只能工作一次。 我也尝试过以编程方式更改它,但没有任何效果(即,我仍然只获得单元格A1和A2)。

这是VBA中处理程序这一部分的部分:

Dim acSpreadsheetType As Integer

strFile2Import = txtFindFile

acSpreadsheetType = ExcelVersion(strFile2Import)

DoCmd.Hourglass (HourglassOn)
DoCmd.SetWarnings (WarningsOff)

DoCmd.RunSQL _
    "DELETE tbl_Work_Plan_Item_Import.*, * " & _
    "FROM tbl_Work_Plan_Item_Import;"

DoCmd.TransferSpreadsheet _
    acImport, acSpreadsheetType, "tbl_Work_Plan_Item_Import", strFile2Import, True

这是“ ExcelVersion”函数:

Public Function ExcelVersion(ByVal strFile2Import As String)
    'https://msdn.microsoft.com/en-us/library/office/ff840717.aspx
    'https://msdn.microsoft.com/en-us/library/office/ff198017.aspx
    Set objapp = CreateObject("Excel.Application")
    objapp.Visible = True
    Set wb = objapp.workbooks.Open(strFile2Import, True, False)
    ExcelVersion = wb.FileFormat
    wb.Close

    objapp.Quit

    Set objapp = Nothing

    Select Case ExcelVersion
        Case 29             'xlExcel3 (Excel3)
            ExcelVersion = 0    'acSpreadsheetTypeExcel3 (Microsoft Excel 3.0 format)
        Case 33             'xlExcel4 (Excel4)
            ExcelVersion = 6    'acSpreadsheetTypeExcel4 (Microsoft Excel 4.0 format)
        Case 39             'xlExcel5 (Excel5)
                            'xlExcel7 (Excel7)
            ExcelVersion = 5    'acSpreadsheetTypeExcel5 (Microsoft Excel 5.0 format)
                                'acSpreadsheetTypeExcel7 (Microsoft Excel 95 format)
        Case 46             'xlXMLSpreadsheet (XML Spreadsheet)
            ExcelVersion = 10   'acSpreadsheetTypeExcel12Xml (Microsoft Excel 2010 XML format)
        Case 50, 51         'xlExcel12 (Excel12)
                            'xlWorkbookDefault (Workbook Default)
            ExcelVersion = 9    'acSpreadsheetTypeExcel12 (Microsoft Excel 2010 format)
        Case 56             'xlExcel8   (Excel8)
            ExcelVersion = 8    'acSpreadsheetTypeExcel8 (Microsoft Excel 97 format)
                                'acSpreadsheetTypeExcel9 (Microsoft Excel 2000 format)
    End Select

End Function

“ strFile2Import”和“ txtFindFile”是从“文件”对话框获取的文件名和要导入的完整路径。

我的原始程序没有ExcelVersion函数,我将acSpreadsheetType硬编码为“ acSpreadsheetTypeExcel12XML”,当我将其更改为“ acSpreadsheetTypeExcel9”后,它可以正常工作一次。

我想念什么?

作为记录,我也尝试在执行导入后对整个表单执行.recalc和.requery,但这也不成功。

你们能为我提供的任何帮助将不胜感激!

"DELETE tbl_Work_Plan_Item_Import.*, * " & _

这有*两次。

改成

"DELETE tbl_Work_Plan_Item_Import.* " & _

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM