1
votes

Imagine a form that, for demontration purposes, contains only a subform and a textbox that need to mirror each other.

I tried to accomplish this by setting the Control Source on the textbox to the value of the field in the subform and this worked to make them mirror each other, but the textbox isn't editable so this is an unsuitable solution.

The next thing I decided to try was using the AfterUpdate event on both controls to run code that sets the value of the other control.

This is easy for the textbox:

'Set value of Notes field on subform whenever value of the corresponding textbox 
Private Sub Notes_Textbox_AfterUpdate()
    Me.subform.Form![Notes] = Me.Notes_Textbox.Text
End Sub

However, this isn't as simple for the subform field. I don't know how to reference an event on a field in a subform in a way that will still allow me to reference controls outside that subform.

As a demonstration, I need a way to do this:

Private Sub subform_Notes_AfterUpdate()
    Me.Notes_Textbox.Text = Me.subform.Form![Notes]
End Sub

I can access the subform fields AfterUpdate event within the scope of the subform but if I do that, then I can't access the Textbox because it is in the main form, not the subform.

So I either need a way to define an event function on a field in a subform from within the scope of the main form, or a way to make the textbox part of the subform while maintaining the subforms ability to open in datasheet view.

1

1 Answers

1
votes

Not sure I get the whole problem (how does datasheet view come into play?), but this should do what you want:

(in the subform)

Private Sub Notes_AfterUpdate()
    Me.Parent!Notes_Textbox.Value = Me![Notes].Value
End Sub

Edit from comment: By far the easiest solution would be a continuous form instead of a datasheet in a subform.

You can have two textboxes, a one-liner in the Details section, and a large one in the form header or footer. Both have the same control source and are automatically updated when the other one is edited. No code required.

If it must be a subform-datasheet, you can use a similar approach: bind the main form to the same recordsource, and in On Current of the subform, move the main form to the same current record.