Question : How do I export an Access Report to Excel and show the text/number fields correctly?

hi there,
I need some help. I hope someone in here can help me.
I am trying to export an Access 2003 report to Excel. The way we do it right now is: you "View" the report, use File -> Export and then select Excel.
The problem is some of the text boxes in the report are showing completely different numbers when exported to Excel. For example, if in the Access report the field contains: " 12 / 12 " (meaning 12 out of 12) in excel this field is showing 40524. I have no idea where that number came from. On some rows the field is displaying correctly.
Also, there is a field which contains a date in the Access report but is exporting as text in the Excel report. How can I fix this?

Thanking you in advance,
cute_stuff

Answer : How do I export an Access Report to Excel and show the text/number fields correctly?

Hello cute_stuff,

In Excel (and Access, for that matter), dates are stored as floating point numbers.  40524 happens to be
12 Dec 2010, so '12 / 12' becoming 40524 makes sense.

Generally speaking, I avoid exporting reports from Access to Excel, because of the issues you raise above,
and more.  Instead, I will typically open a recordset in VBA, and copy the recordset into Excel, and then
format the worksheet as desired.



Sub ExportToExcel()
 
    Dim rs AS DAO.Recordset
    Dim xlApp As Object
    Dim xlWb As Object
    Dim xlWs As Object
    Dim Counter As Long
 
    Const SaveToPath As String = "c:\Results\Report_"
    Const QueryName As String = "NameOfQuery"
 
    Set rs = CurrentDb.OpenRecordset(QueryName)
    Set xlApp = CreateObject("Excel.Application")
    xlApp.DisplayAlerts = False
    Set xlWb = xlApp.ActiveWorkbook
    Set xlWs = xlWb.Worksheets(1)
    With xlWs
        For Counter = 0 To rs.Fields.Count - 1
            .Cells(1, Counter + 1) = rs.Fields(Counter).Name
        Next
        .Cells(2, 1).CopyFromRecordset rs
    End With
    xlWb.SaveAs SaveToPath & Format(Now, "yyyymmdd") & ".xls"
    xlWb.Close False
    xlApp.DisplayAlerts = True
    Set xlWs = Nothing
    Set xlWb = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    rs.Close
    Set rs = Nothing
 
    MsgBox "Done"
 
End Sub



Regards,

Patrick
Random Solutions  
 
programming4us programming4us