Results 1 to 9 of 9
  1. #1
    ADFC is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2014
    Posts
    4

    Export Access Chart to Excel using VBA code

    Hello,



    I would like to export an Access Chart to Excel using VBA code.

    Most specifically, I have a form that shows some data that is obtained from a query.
    In that same form there is a chart that graphs out the query.

    What I want to do, is to export the query and it's associated chart by using a form button with VBA code.

    The query export process is easy.
    For that I used the following code:

    Code:
    Private Sub Command00_Click()
    On Error GoTo Err_Command00_Click
    
    Dim Output1 As Integer
    DoCmd.Requery
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "Query1", "DB_Export", True
    
    Output1 = MsgBox("DB exported successfully!", vbOKOnly, "")
    
    Exit_Command00_Click:
        Exit Sub
    
    Err_Command00_Click:
        MsgBox Err.Description
        Resume Exit_Command00_Click
    
    End Sub

    Now, what code should I include to export the chart as well?
    If it's not possible to export the chart (as a chart element), how can I export the chart as a JPEG into the Excel file "DB_Export.XLSB" that is created with the code provided above?

    Excel File Name = DB_Export.XLSB
    Excel Worksheet Name = Sheet1
    Access Chart Name=Chart1


    Thank you kindly

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Everything returned by a web search concerned exporting chart from Excel. Access can export data to Excel then Excel charting can use the data to render a graph. Or Excel can link to Access query and pull in the data for use by graph.

    Don't think Access can export to jpg format. I think PDF is only graphic format available to Access or any MS Office app.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ADFC is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2014
    Posts
    4
    Hello June7.

    Everything returned by a web search concerned exporting chart from Excel.
    Yes but I want to export from Access to Excel.


    Access can export data to Excel then Excel charting can use the data to render a graph.
    I know, but I would like to automate the process with a single click of a button.
    And please remember that the excel file is generated after I click on my form's button, so there is no simple way for me to have a chart waiting for the query output.


    Or Excel can link to Access query and pull in the data for use by graph.
    Not automated enough, but please correct me if I'm wrong.


    Don't think Access can export to jpg format. I think PDF is only graphic format available to Access or any MS Office app.
    That's why I want to use VBA code.


    Any help?
    Thank you

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    I know you want to export chart to excel, just saying I couldn't find any help reference on the web for that requirement.

    Why not export data to an existing Excel sheet with a chart already developed to use the data? Or link to data from Excel side. Then all the Access VBA has to do is open the Excel file via a hyperlink.

    I don't think any MS Office app can export to graphic format other than PDF, with or without VBA.

    I know it's not what you want to hear but just might be the reality.

    Why do you need data and graph in Excel?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I was going to suggest, "Send your data to Excel and use Excel chart." Maybe make a couple references to Excel and use a template.

    Maybe I won't suggest that now.

  6. #6
    ADFC is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2014
    Posts
    4
    Quote Originally Posted by June7 View Post
    Why not export data to an existing Excel sheet with a chart already developed to use the data? Or link to data from Excel side.
    Yes I ended up taking this suggestion. I only export the raw data, and then, the pre-applied Excel chart does the rest.

    I don't think any MS Office app can export to graphic format other than PDF, with or without VBA.
    Hum... are you sure?
    I am able to export the chart (but in JPEG format) by using the following simple code.

    Code:
    Private Sub Command00_Click()
    On Error GoTo Err_Command00_Click
    Dim Output1 As Integer
    Dim My_Graph1 As Object
    
    DoCmd.Requery
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "Query1", "DB_Export", True
    
    Set My_Graph1 = Me.Graph1.Object
    My_Graph1.Export "C:\Documents\Graph1.jpg", "JPEG"
    Set My_Graph1 = Nothing
    Me.Graph1.Action = acOLEClose
    Output1 = MsgBox("Success", vbOKOnly)
    
    Exit_Command00_Click:
        Exit Sub
    
    Err_Command00_Click:
        MsgBox Err.Description
        Resume Exit_Command00_Click
    
    End Sub
    But for some reason, the code above crashes MS Access, so maybe someone can perfect it a bit more.

    It would still be nice to export the chart as an "Excel Chart Element", but for now, the pre-applied Excel chart does the trick.
    Last edited by ADFC; 04-09-2014 at 04:37 PM.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Of course I can be wrong - couldn't find any code examples on first search. Just found this http://mrvsto.com/2010/07/excel-vba-...t-to-jpg-file/

    I got this to work in Access:

    Forms!MainMenu.Graph1.Export "C:\Temp\Graph1.jpg", "JPEG"

    I also tried BMP but that failed.
    Last edited by June7; 04-09-2014 at 04:08 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If there is a chart object in Excel, you can export that object as a JPG or BMP using VBA.
    https://www.accessforums.net/program...tml#post212827

    If I were looking for a way to link a chart object in Excel to an OLE control on an Access report I would take the above link into consideration.

  9. #9
    trevor40's Avatar
    trevor40 is offline Advanced db Manager
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    402
    If you are after a single click to export your chart to excel, and you are used to vba for access and excel, you could do it all from access by manipulating excel from access. you can create charts, ranges, lists, sheets, anything. to get started record a macro in excel and use that as a reference for the code to use in access to do the same thing. a link between access and excel is required to do this. i have posted some examples of this in other posts.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 04-25-2013, 03:47 PM
  2. Want to Move Excel Chart to Last Tab Using Access VBA
    By JonMulder in forum Programming
    Replies: 1
    Last Post: 03-29-2013, 02:40 PM
  3. VBA code to export from Access to Excel
    By DATADUDE28 in forum Access
    Replies: 1
    Last Post: 11-03-2012, 01:39 PM
  4. Replies: 2
    Last Post: 08-05-2012, 06:32 PM
  5. Replies: 1
    Last Post: 08-12-2010, 10:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums