How do I show or hide a field in form by macro

G

Guest

I can hide or show a field in a form in the properties using visible Yes or
No.
Is it also possible to hide or show fields using a macro ?
 
K

Ken Snell \(MVP\)

There are no fields on a form; just controls. The Visible property is used
to hide/show controls on a form (or a report).

On which object are you wanting to hide "fields"?
 
S

Steve Schapel

Louis,

Just to add to Ken's comments, you can use a SetValue action in a macro
to do this.

For example, suppose you have a textbox named MyTextbox on a form which
you want to hide, the macro would be like this...

Action: SetValue
Item: [MyTextbox].[Visible]
Expression: No

The next thing to decide is what is the appropriate Event you will use
to trigger this action.
 
G

Guest

I'm interested in doing the same type of thing on a form. I would like a
control(2ndReentryDate) to appear AfterUpdate of control ExitDate2. I can
get that to work by using the macro suggestion below.

However, now that control is visible for every record. The usage for me is
a student (record) leaves the program, but I need the option of entering a
re-entry date only for those students that exited. Do I have to re-hide
(un-visible the control) upon record change? Can I get the 're-visibled'
control for that specific record to persist?

Thanks for any help.

Steve Schapel said:
Louis,

Just to add to Ken's comments, you can use a SetValue action in a macro
to do this.

For example, suppose you have a textbox named MyTextbox on a form which
you want to hide, the macro would be like this...

Action: SetValue
Item: [MyTextbox].[Visible]
Expression: No

The next thing to decide is what is the appropriate Event you will use
to trigger this action.

--
Steve Schapel, Microsoft Access MVP
There are no fields on a form; just controls. The Visible property is used
to hide/show controls on a form (or a report).

On which object are you wanting to hide "fields"?
 
S

Steve Schapel

Ben,

It depends whether you have a single view form, or continuous view. If
we are talking single view, then you can re-set the Visible property of
the 2ndReentryDate on the Current event of the form. If it's a
continuous view form, this is not so easy, as you can't have a control
visible for some records and not for others at the same time. If that's
the case, you can think instead in terms of the Enabled property rather
than Visible. If so, you can use Conditional Formatting to control this
record by record, based on whether there is anything entered for
ExitDate2. Another work-around, again using Conditional Formatting, is
to set the the Fore Color and BackColor properties of the control to be
the same as the background colour of the form itself, so it appears to
be invisible.

Let us know if you need any further information about anything I have
mentioned here.
 
G

Guest

Thanks Steve. That get's me a bit closer. One more step.

Now, due to the SetValue, Visible = Yes in the AfterUpdate event of
ExitDate2, 2ndReentryDate appears on the form as needed.

Due to the re-set of 2ndReentryDate to Visible = No in the form's On Current
event, when the form focuses on a different record (I think that is the right
terminology), the 'appeared' control goes away (as it should).

However, when I go back to the record that I entered ExitDate2 for,
2ndReentryDate is still not visible (i.e. the visible doesn't persist). I
get why, because the On Current says "hide it" and its only AfterUpdate that
it says "appear it" (as non-db language as that description is) and I'm not
updating the control (because I already did). What I need now is some way to
say "If ExitDate2 is not Null, then 2ndReentryDate should stay visible."

At this point, I've been doing all of this using the Macro function and
expression builder. I have the sense I'm sliding towards the need or VBA, at
this point.

Thanks for any continued advice.

Ben
 
G

Guest

I forgot to mention that I have a number of subforms being used on this
particular form, so chaning to continuous forms would require a significant
redesign of the form, at this point.
 
S

Steve Schapel

Ben,

VBA would provide no advantage compared with macro, you would still need
to take care of the same thing.

Your macro that runs on the Current event needs to take care of both
possibilities, and set the visibility of the 2ndReentryDate control
based on whether there is data in the ExitDate2. That means this macro
will need two SetValue actions, one to make 2ndReentryDate visible, and
one to hide it. You would put a Condition for these two actions, i.e.
[ExitDate2] Is Not Null
[ExitDate2] Is Null
 
S

Steve Schapel

Ben,

I understand that, and if continuous view forms are the appropriate
usage for your subforms, you should retain this.

It's just one of those facts of life that the same control can not be
visible or not from record to record in a continuous form, so you have
to work around it. As I mentioned before, Conditional Formatting is one
way to work around it, but only based on the Enabled property, not the
Visible property, or else "faking" visibility by masking colours.
 
G

Guest

Steve,

Thank you for your advice. I have still be unable to resolve the issue, but
that is primarily because other programming/design fires popped up, and I
have been unable to return to this on.

If I am able to resolve it, I'll post it.

Ben

Steve Schapel said:
Ben,

VBA would provide no advantage compared with macro, you would still need
to take care of the same thing.

Your macro that runs on the Current event needs to take care of both
possibilities, and set the visibility of the 2ndReentryDate control
based on whether there is data in the ExitDate2. That means this macro
will need two SetValue actions, one to make 2ndReentryDate visible, and
one to hide it. You would put a Condition for these two actions, i.e.
[ExitDate2] Is Not Null
[ExitDate2] Is Null

--
Steve Schapel, Microsoft Access MVP


Ben said:
Thanks Steve. That get's me a bit closer. One more step.

Now, due to the SetValue, Visible = Yes in the AfterUpdate event of
ExitDate2, 2ndReentryDate appears on the form as needed.

Due to the re-set of 2ndReentryDate to Visible = No in the form's On Current
event, when the form focuses on a different record (I think that is the right
terminology), the 'appeared' control goes away (as it should).

However, when I go back to the record that I entered ExitDate2 for,
2ndReentryDate is still not visible (i.e. the visible doesn't persist). I
get why, because the On Current says "hide it" and its only AfterUpdate that
it says "appear it" (as non-db language as that description is) and I'm not
updating the control (because I already did). What I need now is some way to
say "If ExitDate2 is not Null, then 2ndReentryDate should stay visible."

At this point, I've been doing all of this using the Macro function and
expression builder. I have the sense I'm sliding towards the need or VBA, at
this point.
 

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