Dumb Newbie question #2384: how to bind a control to anothercontrol?

R

raylopez99

I hesitate to ask this because it's in the manual somewhere, but since
I'm programming successfully in Visual Basic in Access without any
books (I do have a C#/C++.NET background), I'll ask:

In Access forms, how do you bind a control to another control? I can
bind a control to data in the database, and I can bind a control to
another control that depends on data in the database, but I haven't
yet figured out how to bind a control to another control that is not
bound to the underlying database.

Concrete example (how to do this): a textbox is bound to a checkbox
that is NOT bound to any field in the database, so that when a user on
a form checks the checkbox, something is reflected in the textbox (say
the textbox properties are Unlocked, etc).

What properties do you change in each control (textbox and checkbox)
to make this happen?

As a bonus question, optionally, please explain what extensions do you
put in the code if the two forms are in seperate forms? I've figured
out what "Me." is ('this' keyword in C# / C++), and the extension
"parent" in a parent form / child subform, so if it's possible please
include this information as well. So, my bonus question, to make the
problem more general, is to assume the two controls are in seperate
forms.

Many thanks,

RL
 
K

Klatuu

What a control is bound to is identified by the control's Control Source
property. It can be a field from the form's recordset or it can be an
expression, or it can be based on another control. However, what you are
wanting to do is not considered binding. If you want to populate a value in
a text box based on the value in a check box, you use the After Update event
of the check box. For example, if if both controls are on the same form:

Private Sub MyCheckBox_AfterUpdate()
With Me
If .MyCheckBox = True Then
.MyTextBox = "The Box is Checked"
Else
.MyTextBox = "The Box is not Checked"
End If
End Sub

If they are in different forms, you will first want to ensure the other form
is open:

Private Sub MyCheckBox_AfterUpdate()
With Me
If CurrentProject.AllForms("TheOtherFormName").IsLoaded Then
If .MyCheckBox = True Then
.MyTextBox = "The Box is Checked"
Else
.MyTextBox = "The Box is not Checked"
End If
End If
End Sub

If it is a Check Box on the main form and the text box is on the sub form,
you refer to the name of the subform control on the main form, not the name
of the form being used as a subform. The names can be the same, but it has
to be the name of the control.

Me.MySubFormControl.Form.MyTextBox = .....

And if the check box is on the sub form and the text box is on the main form:

Me.Parent.MyTextBox = .....

Do I get extra points for answering the bonus question?

This also applies to all properties and methods for the text box.
 
R

raylopez99

Damn you're good Klatuu. I'll save this example in my library for
future reference. The part about subforms was a bit confusing but
don't bother clarifying, I'll figure it out from your excellent
example. [See update below]

OT: it seems that with the heavy use of typecasting in VB, it's
perfect for database programming, since C# seems very much like VB
used in Access when used in SQL Server 2005... of course the languages
are converging and the same company designed both languages, so that's
not a coincidence.

RL

UPDATE: before I hit the Send key, I tried your example.

It worked, with one caveat. The part about putting code in a subform
control (I used a checkbox for the subform) to control the parent form
(I used a textbox for the parent) worked flawlessly (well you forgot a
"End With" but that's trivial), however, the second part, putting code
in the parent to control the subform, failed(** see below for an
Update to this update). BTW, this was my experience even before this
post: apparently Visual Basic and Forms in Access doesn't mind going
from sub to parent, but I'm having problems going from parent to sub.

Here's some data as to why: the "intellisense" feature (that
completes the name of the variable/method) did not find the subform
control name while in the After Update procedure of the parent
control. Further, it seems that the subform always has the extension
_subform added to it--did you forget to put this in? So your example
should read: not "Me.MySubFormControl.Form.MyTextBox" but:
-- Me.MySubFormControl_subform.Form.MyTextBox". In my example (I have
a checkbox in the subform, so it's reversed: --
Me.MySubformControl_subform.Form.MyCheckBox---<--up to .Form
Intellisense works, but it *fails* to find MyCheckBox in the subform.
In any event, Intellisense could not make sense of your bonus example
(but it did compile--that's the annoying part of VB--it doesn't have
the extensive compiler checks of C++/C#, but I digress)

Any ideas on the bonus question?

Thanks for your help to date.

*UPDATE TO THE UPDATE:! Stop, I figured it out, and it's quite silly,
as all errors are. It turns out that for a textbox in the parent
being controlled by a checkbox in the subform, the procedure to use
for the 'trigger' should not be "After Update" but something more like
"GotFocus" in the parent textbox (I don't know why, since the
opposite, going from code in subform control controlling a control in
parent, actually does work fine with AfterUpdate). When I moved the
same code in the parent control (which compiled) to "GotFocus" rather
than "After Update", it worked to produce a message in the parent
textbox when I clicked on the subform checkbox.

BTW, my version of Access does add an extension "_subform" to the name
of the subform (which you right click on the outside of the subform
outline to get), and this extension is part of the subform name that
has to be added.
 
R

raylopez99

This is the AutoCorrect feature in Access. You might want to read about at
Allen Browne's site. When you name an object and leave a space between words,
AutoCorrect will change the "space" to a "underscore". Space is a reserved
character in Access.

Yes, thanks, I figured that out after posting. Like Intellisense in
C# and C++.NET. Without it, I and most other people would be lost,
there being so many objects and functions available at any one time.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top