Solved

ms access save a reports chart as a jpg

Posted on 2008-10-28
24
1,976 Views
Last Modified: 2013-11-28
I have a form with a button.  I will like to click the button, have it open a report that contains a chart.  Then export that chart to a jpg file.  The current code crashes access when I close the opened report.  This works with a form with a chart but I can't get it to work with a report.  Any ideas?
Private Sub btnimg_Click()
 
    sDirectoryName = BrowseDirectory("Find and select where to export the Image file.")
    txtFileLocation = sDirectoryName
 
    strRpt = "MonthlyCostByGroupings"
    DoCmd.OpenReport strRpt, acViewPreview
    Dim grpApp As Object
    Set grpApp = Reports!MonthlyCostByGroupings.OLEUnbound0.Object
    grpApp.Export txtFileLocation & "\Chart.jpg"
    Reports!MonthlyCostByGroupings.OLEUnbound0.Locked = False
    Reports!MonthlyCostByGroupings.OLEUnbound0.Enabled = True
    Set grpApp = Nothing
    Reports!MonthlyCostByGroupings.Action = acOLEClose
End Sub

Open in new window

0
Comment
Question by:bdead20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 11
24 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22826349
bdead20

1. Please post any "Support" Code.
There is a reference to a "BrowseDirectory" function, yet there is no code for it?

2. Niether strRpt nor sDirectoryName is explicitly declared.

I am trying to duplicate your issue, so I need this info.

JeffCoachman
0
 

Author Comment

by:bdead20
ID: 22830486
That code isn't specific to the problem.  I should have cleaned it up more before posting.  I updated the code to not utilize those functions and attatched it.
Private Sub btnimg_Click()
    DoCmd.OpenReport "MonthlyCostByGroupings", acViewPreview
    Dim grpApp As Object
    Set grpApp = Reports!MonthlyCostByGroupings.OLEUnbound0.Object
    grpApp.Export "C:\Chart.jpg"
    Reports!MonthlyCostByGroupings.OLEUnbound0.Locked = False
    Reports!MonthlyCostByGroupings.OLEUnbound0.Enabled = True
    Set grpApp = Nothing
    Reports!MonthlyCostByGroupings.Action = acOLEClose
End Sub

Open in new window

0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22832967
I'll look at this tonight.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22837900
I could not duplicate the issue.
How are you creating a Chart/Graphe as an OLEUnbound Object?
0
 

Author Comment

by:bdead20
ID: 22840404
I threw together a simple DB that causes the error.
Save-JPG-DB.zip
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22847433
Again, my question is:

How are you creating your chart to be an image in an OLE Unbound Object Frame?

When you create a chart using the Wizard, the Object is simply: "Chart"

JeffCoachman
0
 

Author Comment

by:bdead20
ID: 22849464
I've tried creating with the wizard, and just adding an Unbound Object Frame.  Both with the object type of "Microsoft Graph Chart".  I get the error either way.

My understanding is you can export this type as a image.  That part is actually working (in that the image is created) but the problem is the crashing of access after the image is saved to the directory.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22850966
bdead20,

I undersand that (I am getting the same result), but again, my question is *HOW* are you taking the "Chart Object" (which is what Access creates by default), and putting it into a OLE Unbound control?
Please explain.
Perhaps this has something to do with why it is not working.

JeffCoachman
0
 

Author Comment

by:bdead20
ID: 22851198
Maybe I don't understand the question.  I do the following to create the chart.

1. I click create new report
2. Select design mode
3. Click "Unbound Object Frame"
4. Click in Detail
5. Select create New Microsoft Graph Chart
6. Save report

That's all I do to create the report.   I thought that makes it an OLE Unbound Control automatically.  Maybe that's a step I'm not taking that I should be?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22852488
bdead20,

Yes that is what I meant.
;-)

The steps to create a standard Chart are:
1. I click create new report
2. Select design mode
3. Click Insert-->Chart
5. Select create New Microsoft Graph Chart
6. Save report

So the startup is different, I will investigate this tonight.

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22865770
I amd getting the same errors as you.

Can you post the Form version of this, so I can see how it works successfully?
0
 

Author Comment

by:bdead20
ID: 22871721
Hmm, I can't seem to get it to work with the form either now.  I'll mess around with it some more and figure out how I got to work and put it up tomorrow.
0
 

Author Comment

by:bdead20
ID: 22939532
I'm still not able to figure this out.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22940999
Can you post the code that you were actually using to do this with a form?
0
 

Author Comment

by:bdead20
ID: 22942854
I can't seem to get that to work either.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22947338
Can you post it anyway?
Perhaps I can.
0
 

Author Comment

by:bdead20
ID: 22963946
Here is aform that has 2 buttons.  1 tries to save from a report and the other from a form.  Also on the other form is a button that tries saving an image from itself.  All three encounter the same error of creating the image fine but then crashing access.
Save-JPG-DB.zip
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22973338
bdead20,

The code on the "GraphForm" will work with this code, however it will still throw an OLE error and crash out of Access.

Private Sub Command1_Click()
Dim grpApp As Object
   Set grpApp = Me.OLEUnbound0 '.Object
    grpApp.Export "C:\ChartFromForm.jpg"
End Sub

Where did you get this code from, originally?

JeffCoachman
0
 

Author Comment

by:bdead20
ID: 22975305
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 22981106
bdead20,

Well that code worked beautifully.

But again, it is using the default "Chart object".
This brings me back to my original question:
Why are you using an OLEUnbound control, when the default is a "Chart Object"?
This may explain why it worked for you before, but now crashes when you use this "OLEUnbound" control.
Besides the charts you are using in the sample have no source data, so I am not sure if this would have ever worked anyway.

When you create a chart in Access, follow the basic wizard steps:
Open the Form or Report in design View.
Click: Insert-->Chart
Size your chart
Select your Table/Query
Select your Fields
Select your Chart Type
Select your Chart Layout
Name your Chart
Finish
... and the code in the link you provided will work fine.

I tried everything I could, but could not get the code to work in a report.
The only thing I can see to do, is to create a temp form with the exact same chart as the report, and then run the conversion code on the temp form.

JeffCoachman
0
 

Author Comment

by:bdead20
ID: 22984295
Ok, yes, the way I was creating charts was definitly the problem.  I couldn't get it to work with a report either.  This is probably because the Enabled and Locked options are not avaialbe for a chart inside a report and required to prevent the error.  Thanks for you help and sticking with the problem.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22988595
bdead20,

  "probably because the Enabled and Locked options are not avaialbe for a chart"

Yeah, I thought about that too.

Even when when I commented out that code, the JPG, would still be created, but then the form/report would trigger the crash.

JeffCoachman
0
 

Author Comment

by:bdead20
ID: 22993991
I have a follow up question related to this one located at
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_23916092.html#a22993978
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22995013
thanks
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question