Access table data to Excel

前端 未结 5 1659
傲寒
傲寒 2020-12-18 13:07

I have a problem I have got stuck on.

I want to export my Access table to an Excel file. Currently, I do that using DoCmd.TransferSpreadsheet, but I wa

相关标签:
5条回答
  • 2020-12-18 13:10

    We have a generic function that will export our continuous forms directly to Excel. You'll need to add the Microsoft Excel Library in the available tools to have it work from your code. This function can be made available to users in all your forms. If you want to export directly from a table, you can easily adapt it.

    Public Function ExportToExcel(x_frm as Form)
    
    Dim ctl As Control, _
        xlApp As Excel.Application, _
        xlBook As Excel.Workbook, _
        xlSheet As Excel.Worksheet, _
        columnName() As String, _
        columnCount as Integer
    
    'suppose Excel is not opened. You can add an extra control for that'
    Set xlApp = CreateObject("Excel.Application")
    
    'create an Excel workbook, declare the first sheet'
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
    xlApp.Visible = False
    
    columnCount = fc().section(0).Controls.Count
    
    'use array columnName() to collect the column names of detail section in tabbed order'
    ReDim columnName(columnCount)
    
    For Each ctl In fc().section(0).Controls
        columnName(ctl.TabIndex) = ctl.Name
    Next ctl
    
    'This function will add a title to the excel sheet with my favorite formating'
    'I can for example decide this title to be bold/Arial/16 on cell(B2)'
    addTitleToExcelSheet xlSheet, x_frm.Name
    
    'This function will add the column names to my Excel sheet with my favorite formating'
    'for example column names will be added on row 4, columns B to B + columnCount'
    addColumnNameToExcelSheet xlSheet, columnName()
    
    'This function will add the column values to my Excel sheet with specific format (date, number, etc)'
    'Data will be added to the range defined by '
    'row 5 to row 5 + x_frm.recordset.recordcount, '
    'columns B to B + columnCount.'
    'Recordset values will have to be read according to tab order'
    'exported data can depend on recordset filter and orderBy property: your choice'
    addColumnValueToExcelSheet xlSheet, columnName(), x_frm.Recordset
    
    'The Excel sheet is made visible and saved under the current folder with the forms name'
    xlApp.Visible = True
    xlBook.SaveAs x_frm.Name & ".xls"
    
    Set xlBook = Nothing
    Set xlSheet = Nothing
    Set xlApp = Nothing
    
    End Function
    

    Here is a view of the result. Left is Access form, right is Excel exportToExcel result. Hope you like it.

    alt text

    0 讨论(0)
  • 2020-12-18 13:23

    If you use DoCmd.TransferSpreadsheet and create an original and then edit it so that the formatting is correct, you can then run DoCmd.TransferSpreadsheet again and it will update the file with the values but keep the formatting.

    However, if a human then edits the file by adding new tabs, or adding calculations, etc, then the DoCmd.TransferSpreadsheet will no longer work and will fail with an ugly error message. So what we do in our enviroment is DoCmd.TransferSpreadsheet to an original file with formatting, and follow that up by copying the file to the users desktop, and then opening that copy so the user doesn't disturb the original file.

    This approach is a minimum code, clean, and easy to maintain solution. But it does require a extra "source" or original file to be hanging around. Works in Access 2007.

    0 讨论(0)
  • 2020-12-18 13:30

    This Excel macro retrieves data from your MS Access database:

    Sub Makro1()
    ''
    Const sDB = "c:\db1.mdb"
    Const sSQL = "SELECT * FROM Table1"
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            "ODBC;DSN=MS Access-database;DBQ=" + sDB + ";FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
            , Destination:=Range("A1"))
            .CommandText = Array(sSQL)
            .Name = "Query1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = True
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .Refresh BackgroundQuery:=False
        End With
    End Sub
    
    0 讨论(0)
  • 2020-12-18 13:32

    I don't really have any experience with this domain, but please note that Excel has a row limit that is way smaller than that of Access.

    0 讨论(0)
  • 2020-12-18 13:34

    It might be easier to start with an Excel spreadsheet that is preconfigured with your formatting. It also might be easier to use VBA from within excel, and pull the data from Access (and format it) rather than push from Access. Excel VBA will be better prepared to do your Excel formatting for you. And one is about as easy as the other. If you're trying to do this with Macros alone, it's still probably easier with Excel macros than Access macros.

    0 讨论(0)
提交回复
热议问题