Visual Basic:导出对Excel表的访问| VBA

时间:2015-01-23 07:04:59

标签: vba excel-vba access-vba excel

我的项目是45%我需要帮助才能将导出访问数据转换为Excel电子表格, " *"是我有问题的代码,我使用Visual Studio 2013

我打算将2013年表格导出到excel 2013.

'将访问数据库中的表格导出为EXCEL

    Dim strExcelFile As String
    Dim strWorksheet As String
    Dim strDB As String
    Dim strTable As String
    * Dim objDB As Database

    strExcelFile = "|DataDirectory|\ExportedData.xls"
    strWorksheet = "WorkSheet1"
    strDB = "C:\My Documents\MyDatabase.mdb"
    strTable = "MyTable"

    * objDB = OpenDatabase(strDB)

    If Dir(strExcelFile) <> "" Then Kill(strExcelFile)

    objDB.Execute _
      "SELECT * INTO [Excel 8.0;DATABASE=" & strExcelFile & _
       "].[" & strWorksheet & "] FROM " & "[" & strTable & "]"
    objDB.Close()
    objDB = Nothing

新代码

在acExport上出错,您认为此变量用于什么。

&LT;

    Dim objDB As Object
    Dim strExcelFile As String
    Dim strWorksheet As String
    Dim strDB As String
    Dim strTable As String
    strExcelFile = "|DataDirectory|\ExportedData.xls"
    strWorksheet = "sheet1"
    strDB = "|DataDirectory|MyDatabase.mdb"
    strTable = "MyTable"

    objDB.OpenCurrentDatabase(strDB)
    objDB.DoCmd.TransferSpreadsheet(acExport, 8, strTable, strExcelFile, True, strWorksheet)

    objDB.CloseCurrentDatabase()
    objDB = Nothing

&GT;

1 个答案:

答案 0 :(得分:0)

Set objDB = OpenDatabase(strDB)

在为对象类型变量赋值时,应使用Set