简体   繁体   中英

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).

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.

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?

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. 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 "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.

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.

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.

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:

? 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.

You don't have to specify the name of the parent form.

Me.Parent.CONTROLNAME.Value is a kind of working code.

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 .

Me.Parent.Forms.formA actually igual to Me because you were calling :

subform (A)'s textbox 'after update' event

It should be formB and the word "Form" should be placed after (and the word "Value" isn't mandatory):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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