vba 无法在 Report_Open 中更改 TextBox 的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8614268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't change Text of TextBox in Report_Open
提问by Henrik Erlandsson
All I want to do is set a textbox or label's text to something dynamic when the report is opened with a click of a button in another form. I've solved everything except actually changing the text.
我想要做的就是在通过单击另一种形式的按钮打开报告时将文本框或标签的文本设置为动态内容。除了实际更改文本之外,我已经解决了所有问题。
This code gives run-time error 2478 on SetFocus:
此代码在 SetFocus 上给出运行时错误 2478:
Me.tFilial.SetFocus
Me.tFilial.Text = filialen
Without SetFocus I get a run-time error saying the text can't be changed without switching control to the control in question.
如果没有 SetFocus,我会收到一个运行时错误,指出如果不将控件切换到相关控件,就无法更改文本。
What is allowed where is always in question in Access, it seems. How do I solve this? Can I set the value on the buttonclick in the other form with
似乎在 Access 中总是有问题的地方允许什么。我该如何解决这个问题?我可以在其他表单中设置 buttonclick 上的值吗
Reports![rptPressSchema]![tFilial].text="Hello"?
I would be happy to use a label instead, if that solves it. But the bottom line is I can try to do this every which way but I thought I'd ask you for advice as to best practice, as this must be a very common task indeed.
如果可以解决问题,我很乐意改用标签。但最重要的是,我可以尝试以任何方式执行此操作,但我想我会向您征求有关最佳实践的建议,因为这确实是一项非常常见的任务。
采纳答案by mwolfe02
From the Access help:
从 Access 帮助:
While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again.If you use the Save Record command on the Records menu to save the data in the control without moving the focus, the Text property and Value property settings will be the same.
当控件具有焦点时,Text 属性包含当前在控件中的文本数据;Value 属性包含控件上次保存的数据。当您将焦点移到另一个控件时,该控件的数据会更新,并且 Value 属性被设置为这个新值。在控件再次获得焦点之前,Text 属性设置将不可用。如果在不移动焦点的情况下使用“记录”菜单上的“保存记录”命令将数据保存在控件中,则 Text 属性和 Value 属性设置将相同。
Basically, the .Text
property serves no purpose in a Report because individual controls cannot receive the focus. However, as @Remou stated in his comment, you can simply replace .Text
with .Value
and your code should work fine (no need to set focus when updating the value).
基本上,该.Text
属性在报告中没有任何作用,因为单个控件无法获得焦点。但是,正如@Remou 在他的评论中所述,您可以简单地替换.Text
为.Value
,您的代码应该可以正常工作(更新值时无需设置焦点)。
回答by ZY Huang
spent a lot of time for searching and trying. Finally figure the things out... To dynamically set the TextBox content, it is convenient to use tbTest.Value = "hello"
花了很多时间去寻找和尝试。终于搞清楚了...要动态设置TextBox的内容,使用tbTest.Value = "hello"很方便
but the trick is if you are using this On Open
, it will be in trouble...
但诀窍是如果你使用它On Open
,它会遇到麻烦......
Run-time error '2448'
You can't assign a value to this object.
Run-time error '2448'
You can't assign a value to this object.
So you need set the value On Load
所以你需要设置值 On Load
Private Sub Report_Load()
Me.tbTest.Value = "hello"
End Sub
Private Sub Report_Open(Cancel As Integer)
'Me.tbTest.Value = "hello"
End Sub
I see nowhere for any explanation of this, my guess is for Open event, the object is still not initiated (documentfor explaining Load and Open event)...
我找不到对此的任何解释,我的猜测是针对 Open 事件,对象仍未启动(用于解释 Load 和 Open 事件的文档)...