简体   繁体   English

MS Access如何从子窗体获取另一个子窗体的值?

[英]MS Access how can a subform get the value from another subform?

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. 因此,对于Access来说,我做了一些谷歌搜索,看起来这可以用一些VBA实现这一点。 So on subform (A)'s textbox 'after update' event I have added this 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. 我甚至不确定如何在Access中调试VBA。 Can someone help me out? 有人可以帮我吗?

Thanks! 谢谢!

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. 如果ID绑定到自动编号字段,则db引擎将在您添加新记录时提供其值。 However, since the user didn't make that change, the after update event does not fire. 但是,由于用户未进行此更改,因此不会触发更新后事件。

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. 在VB编辑器的立即窗口中查看Debug.Print的输出。 You can go there from your main Access window with the Ctrl+g keyboard shortcut. 您可以使用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. 右键单击代码行,然后从上下文菜单中选择Toggle-> Breakpoint。 Or click in the left margin so that a reddish dot appears to mark the breakpoint. 或者单击左边距,以便出现红点以标记断点。 Or press the F9 key. 或按F9键。 Or choose Debug->Toggle Breakpoint from the VB Editor's main menu. 或者从VB Editor的主菜单中选择Debug-> Toggle Breakpoint。

You can also type Stop on a line by itself to trigger break mode. 您也可以Stop在一行上键入Stop以触​​发中断模式。 However, you would need to remove it later. 但是,您需要稍后将其删除。 Those other breakpoints I mentioned are temporary and don't get saved in the code module. 我提到的那些其他断点是临时的,不会保存在代码模块中。

However you get to break mode, you can then step through the code one line at a time with the F8 key. 但是,如果您进入中断模式,则可以使用F8键一次一行地执行代码。 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: 您还可以在中断模式下随时通过键入问号,变量名称,然后在立即窗口中输入Enter键来检查变量的值:

? MyVariable

Explore the VB Editor's main menu to find additional debugging options. 浏览VB Editor的主菜单以查找其他调试选项。 For example, the Watch Window will allow you to monitor the values of selected variables as you step through the code. 例如,Watch Window将允许您在单步执行代码时监视所选变量的值。

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 问候

On the update property of the textbox you are changing, put the following: 在您要更改的文本框的更新属性上,输入以下内容:

[Forms]![frmContainingValueYouWantToChange]![txtTextboxYouWantToChange] = "newValueHere"

Let us know how you get on? 让我们知道您的身体情况如何?

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并且“Form”一词应该放在(并且“Value”这个词不是强制性的)之后:

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 问候

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

相关问题 访问VBA:子窗体无法从其他子窗体获取值 - Access VBA: A subform can't get values from other subform 如何在MS Access中引用子表单 - How to reference a subform in MS Access MS Access VBA-从另一个子表单过滤子表单(在主表单的导航中) - MS Access VBA- Filter subform from another subform (within a navigation from of a main form) MS Access筛选器子表单(DataSheet)与另一个子表单(列表) - MS Access Filter Subform (DataSheet) with another Subform (List) 在另一个模块MS Access VBA中引用子窗体的子窗体控件 - Referencing a Subform's Subform Control in another Module MS Access VBA MS Access - 将数据从子窗体传递到另一个窗体 - MS Access - Passing Data from a Subform to another Form MS ACCESS:使用另一个表中的数据填充子窗体 - MS ACCESS: Populate a subform with data from another table 在子表单中访问子表单我无法访问字段值 - Access Subform in a Subform I can not access to the field value 如何访问子窗体上的文本框控件的值,其中子窗体本身只是主窗体的另一个子窗体上的控件 - How do I access the value of a textbox control on a subform where the subform itself is just a control on another subform of the main form 如何在MS Access的子窗体上应用多个过滤器? - How can I apply multiple Filters on my Subform on MS Access?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM