vba MS Access 如何从另一个子表单获取子表单的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7989805/
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
MS Access how can a subform get the value from another subform?
提问by Nick
I have a main form that contains two other child subforms. When a textbox value changes on subform (A) I would like to set the value of a textbox on subform (B) equal to the changed textbox on subform (A).
我有一个包含另外两个子表单的主表单。当子窗体 (A) 上的文本框值更改时,我想将子窗体 (B) 上的文本框的值设置为等于子窗体 (A) 上更改后的文本框。
So being completely new to Access I did some googling and it looks like this can accomplish this with some VBA. So on subform (A)'s textbox 'after update' event I have added this VBA.
因此,作为 Access 的新手,我进行了一些谷歌搜索,看起来这可以通过一些 VBA 来完成。因此,在子表单 (A) 的文本框“更新后”事件中,我添加了此 VBA。
Private Sub ID_AfterUpdate()
Me.Parent.Forms.formA.textboxDestination.Value = Me.textboxSource.Value
End Sub
I am doing this using the code builder. Then I save and run the form. Nothing happens. No errors.. nothing I am not even sure that piece of code is running. I'm not even sure how one debugs VBA in Access. Can someone help me out?
我正在使用代码生成器执行此操作。然后我保存并运行表单。没发生什么事。没有错误.. 没什么我什至不确定那段代码正在运行。我什至不确定如何在 Access 中调试 VBA。有人可以帮我吗?
Thanks!
谢谢!
采纳答案by HansUp
The after update event for a control fires when the user changes its value. If ID is bound to an autonumber field, the db engine will supply its value when you add a new record. However, since the user didn't make that change, the after update event does not fire.
控件的更新后事件在用户更改其值时触发。如果 ID 绑定到自动编号字段,则数据库引擎将在您添加新记录时提供其值。但是,由于用户没有进行该更改,因此不会触发 after update 事件。
For general debugging purposes, you can add a Debug.Print or a MsgBox statement.
对于一般调试目的,您可以添加 Debug.Print 或 MsgBox 语句。
Debug.Print "my event fired"
MsgBox "my event fired"
View the output from Debug.Print in the Immediate Window of the VB Editor. You can go there from your main Access window with the Ctrl+g keyboard shortcut.
在 VB 编辑器的立即窗口中查看 Debug.Print 的输出。您可以使用 Ctrl+g 键盘快捷键从主 Access 窗口转到那里。
Another technique is to set a break point on a line of your code. Right-click on the code line, then choose Toggle->Breakpoint from the context menu. Or click in the left margin so that a reddish dot appears to mark the breakpoint. Or press the F9 key. Or choose Debug->Toggle Breakpoint from the VB Editor's main menu.
另一种技术是在您的代码行上设置断点。右键单击代码行,然后从上下文菜单中选择 Toggle->Breakpoint。或者单击左边距,以便出现一个标记断点的红点。或按 F9 键。或者从 VB Editor 的主菜单中选择 Debug->Toggle Breakpoint。
You can also type Stop
on a line by itself to trigger break mode. However, you would need to remove it later. Those other breakpoints I mentioned are temporary and don't get saved in the code module.
您也可以单独Stop
在一行上键入以触发中断模式。但是,您稍后需要将其删除。我提到的其他断点是临时的,不会保存在代码模块中。
However you get to break mode, you can then step through the code one line at a time with the F8 key. That will show you which lines are executed. You could also inspect the value of a variable at any time in break mode by typing a question mark, then variable name, and then the Enter key in the Immediate Window:
无论您如何进入中断模式,您都可以使用 F8 键一次一行地单步执行代码。这将显示您执行了哪些行。您还可以在中断模式下随时通过在立即窗口中键入问号、变量名称和 Enter 键来检查变量的值:
? MyVariable
Explore the VB Editor's main menu to find additional debugging options. For example, the Watch Window will allow you to monitor the values of selected variables as you step through the code.
浏览 VB 编辑器的主菜单以查找其他调试选项。例如,监视窗口将允许您在逐步执行代码时监视所选变量的值。
回答by JMK
On the update property of the textbox you are changing, put the following:
在您要更改的文本框的更新属性上,输入以下内容:
[Forms]![frmContainingValueYouWantToChange]![txtTextboxYouWantToChange] = "newValueHere"
Let us know how you get on?
让我们知道您的身体情况如何?
回答by PHILIPPE HANCHIR
You don't have to specify the name of the parent form.
您不必指定父表单的名称。
Me.Parent.CONTROLNAME.Value is a kind of working code.
Me.Parent.CONTROLNAME.Value 是一种工作代码。
Regards
问候
回答by Lionel T.
I know it was 6 years ago, but I think the error here was that you were updating value from the formA
to the same formA
.
我知道这是6年前,但我觉得这里的错误是,你是从更新值formA
相同的formA
。
Me.Parent.Forms.formA
actually igual to Me
because you were calling :
Me.Parent.Forms.formA
实际上是Me
因为你在打电话:
subform (A)'s textbox 'after update' event
子表单 (A) 的文本框“更新后”事件
It should be formB
and the word "Form" should be placed after (and the word "Value" isn't mandatory):
它应该是,formB
并且“形式”这个词应该放在后面(并且“值”这个词不是强制性的):
Private Sub textboxSource_AfterUpdate()
Me.Parent.formB.Form.textboxDestination.Value = Me.textboxSource.Value
End Sub
You can learn here some more informations how to Refer to Form and Subform properties and controls
您可以在此处了解更多有关如何参考表单和子表单属性和控件的信息
Regards
问候