Article ID: 926429 - Last Review: May 1, 2009 - Revision: 3.0
Error message when you try to programmatically export a Microsoft Graph object in Access 2007: "Run-time error 1004: Application-defined or object-defined error"
Method 1: Create a PivotChart object instead of a Graph object"
Start Access 2007.
Click the Microsoft Office Button, and then click Open.
In the Open dialog box, select the database that you want, and then click Open.
On the Create menu, click PivotChart.
On the Design menu, click Field List in the Show/Hide group. Do this to open the Chart Field List list.
Move CategoryName from Chart Field List to Drop Category Fields Here.
Move ProductSales from Chart Field List to Drop Data Fields Here.
Note The chart displays the sum of product sales by category.
Click the Microsoft Office Button, and then click Close.
In the Save As dialog box, type frmPivotChart under Form Name, and then click OK.
On the Create menu, click Form Design in the Forms group, and then move frmPivotChart from the Navigation Pane to the designer.
Note This procedure creates a subform.
Add a command button to the main form, and then set the Name property of the command button to Command1.
On the Design menu, click Button in the Controls group, and then click the Form2 designer to put the Command button on the form.
Right-click Command1, and then click Properties.
In the Property Sheet dialog box, click the Event tab, and then click the ellipsis button (...) for the On Click event.
In the Choose Builder dialog box, select Code Builder, and then click OK.
In Visual Basic Editor, replace the code with the following code.
Option Compare Database
Private Sub Command1_Click()
Dim frm As Access.Form
Set frm = Me.frmPivotChart.Form
frm.ChartSpace.ExportPicture "C:\<folder name>\PivotChart1.jpg", "JPEG"
End Sub
On the File menu, click Close and Return to Microsoft Office Access.
In the Save dialog box, click Yes.
In the Save As dialog box, type frmMain under Form Name, and then click OK.
On the Home menu, click View in the Views group, and then click Form View.
The subform appears in PivotChart view.
On the frmMain form, click the Command1 button to export the PivotChart to the following file:
C:\PivotChart1.jpg
On the Home menu, click View in the Views group, and then click Design View.
Method 2: Set the action property to acOLEClose after the object is exported
Use the following code to export a graph object in Microsoft Access 2007
Private Sub Command1_Click()
Dim grpApp As Graph.Chart
Set grpApp = Me.Graph1.Object
grpApp.Export "C:\<folder name>\Graph1.jpg", "JPEG"
Me.Graph1.Enabled = True
Me.Graph1.Locked = False
Set grpApp = Nothing
Me.Graph1.Action = acOLEClose
End Sub
Click the Microsoft Office Button, and then click New.
Click Blank Database, type DatabaseName in the File Name box, and then click Create.
On the Create menu, click Table Design in the Tables group, and then create the following table:
Collapse this tableExpand this table
Field Name
Data Type
CategoryName
Text
ProductSales
Currency
Click the Microsoft Office Button, and then click Save.
Type tabSales in the Table Name box, and then click OK.
Note When you click OK, you may create a primary key.
In the Views group, click the Datasheet view, and then type the following values in the CategoryName and ProductSales fields:
Collapse this tableExpand this table
CategoryName
ProductSales
Beverages
45000
Condiments
15000
Produce
65000
Seafood
33000
On the Create menu, click Form Design in the Forms group.
On the Design menu, click the Chart control in the Controls group, and then click the Form1 designer.
In the Chart Wizard dialog box, click Next.
Note Table:tabSales is automatically selected under Which table or query would you like to use to create your chart.
In Chart Wizard under Which fields contain the data you want for the chart, select CategoryName under Available Fields.
Click > to move CategoryName under Fields for Chart, and then click Finish.
Repeat steps 11 and 12 for ProductSales.
Right-click the chart, and then click Properties.
On the Design menu, click Button in the Controls group, and then click the Form1 designer.
Click Cancel to close Command Button Wizard.
Right-click the Command1 control, and then click Build Event.
In Visual Basic Editor, replace the existing code with the following code.
Option Compare Database
Private Sub Command1_Click()
Dim grpApp As Graph.Chart
Set grpApp = Me.Graph0.Object
grpApp.Export "C:\Graph0.jpg", "JPEG"
Set grpApp = Nothing
End Sub
On the Tools menu, click References.
In the References - DatabaseName dialog box, click to select Microsoft Graph 12.0 Object Library, and then click OK.
On the File menu, click Close and Return to Microsoft Office Access.
In the Views group, click View, and then click Form View.
On Form1, click Command1.
Note The problem that is discussed in the "Symptoms" section occurs.