简体   繁体   English

如何使用VBA更改Access报告中的图表标题?

[英]How can I change the Chart Title in an Access report with VBA?

I have an Access Report with a chart and I want to change the title of the chart when the report is opened (preview or print). 我有一个带有图表的Access报告,我想在打开报告(预览或打印)时更改图表的标题。 I found the following code: 我找到了以下代码:

Dim g As Graph.Chart
Set g = Me!Graph0.Object
With g
    .ChartTitle.Text = "Test"
End With

But I don't know with which event I should run this code. 但我不知道我应该运行这个代码的事件。 I tried Report_Open and other events but it never changes the title. 我尝试了Report_Open和其他事件,但它永远不会更改标题。

I am not sure if your code works, but to trigger an event on opening a report you need to put the code into the Report.Load event. 我不确定您的代码是否有效,但是要在打开报表时触发事件,您需要将代码放入Report.Load事件中。

Check out msdn: https://msdn.microsoft.com/en-us/library/office/ff197739.aspx 查看msdn: https//msdn.microsoft.com/en-us/library/office/ff197739.aspx

I found the answer here: 我在这里找到了答案:
Modify Chart properties in Access report via VBA (error 2771) 通过VBA修改Access报告中的图表属性(错误2771)

First I copied the chart from a report to a form and tested the code to change the title. 首先,我将图表从报表复制到表单并测试代码以更改标题。 That worked. 那很有效。
Then I tried to run the code with the open or load event in the report and I always received an error. 然后我尝试在报告中使用open或load事件运行代码,我总是收到错误。 I googled that error and found the article above and that was the solution. 我用谷歌搜索了这个错误并找到了上面的文章,这就是解决方案。
I just added a button to the report and added the line 我刚刚在报告中添加了一个按钮并添加了该行

Me.Command1.SetFocus

at the beginning of my code. 在我的代码的开头。
Now I can run the code from Report_Load() or Report_Open(). 现在我可以运行Report_Load()或Report_Open()中的代码。 It works for both. 它适用于两者。 Problem solved. 问题解决了。

如果使用表单,请使用OnActivate事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM