Show Hide field on a form based on condition

K

Khalil handal

A field in a table about marial status, Yes / No field
Is shown on a form as a check box.

If the check box for this field is checked (answer is yes), I want the other
two field (name of spouse and number of males and female children) to be
shown on the form so that the user can fill in the requested info.

how can this be done?
 
K

Ken Sheridan

In both the check box's AfterUpdate event procedure put code like this:

If Me.[YourCheckBox] Then
Me.[Spouse].Visible = True
Me.[NumberOfChildren].Visible = True
Else
Me.[Spouse].Visible = False
Me.[NumberOfChildren].Visible = False
Me.[Spouse] = Null
Me.[NumberOfChildren] = Null
End If

In the form's Current event procedure put:

If Me.[YourCheckBox] Then
Me.[Spouse].Visible = True
Me.[NumberOfChildren].Visible = True
Else
Me.[Spouse].Visible = False
Me.[NumberOfChildren].Visible = False
End If

In the first case the code also sets the values of the two controls to Null.
This is in case any data has inadvertently been entered in those controls
prior to the check box being unchecked. Otherwise those values would still
be present in the underlying fields, even though not seen on the form, and
could cause incorrect results when querying the database. In the second case
this isn't necessary as the code there is merely to reflect the current value
of the marital status field.

Note that this only works satisfactorily in single form view. In a
continuous form the two controls would be shown/hidden in all rows depending
on the values in the currently selected row.

Ken Sheridan
Stafford, England
 
K

Khalil Handal

Thanks it work well with one adjustment in the default value on the table
level were i needed to put the default value to "NO" or I will had an error
of wrong usage of null.

It is now working fine.
ThankYou


Ken Sheridan said:
In both the check box's AfterUpdate event procedure put code like this:

If Me.[YourCheckBox] Then
Me.[Spouse].Visible = True
Me.[NumberOfChildren].Visible = True
Else
Me.[Spouse].Visible = False
Me.[NumberOfChildren].Visible = False
Me.[Spouse] = Null
Me.[NumberOfChildren] = Null
End If

In the form's Current event procedure put:

If Me.[YourCheckBox] Then
Me.[Spouse].Visible = True
Me.[NumberOfChildren].Visible = True
Else
Me.[Spouse].Visible = False
Me.[NumberOfChildren].Visible = False
End If

In the first case the code also sets the values of the two controls to
Null.
This is in case any data has inadvertently been entered in those controls
prior to the check box being unchecked. Otherwise those values would
still
be present in the underlying fields, even though not seen on the form, and
could cause incorrect results when querying the database. In the second
case
this isn't necessary as the code there is merely to reflect the current
value
of the marital status field.

Note that this only works satisfactorily in single form view. In a
continuous form the two controls would be shown/hidden in all rows
depending
on the values in the currently selected row.

Ken Sheridan
Stafford, England

Khalil handal said:
A field in a table about marial status, Yes / No field
Is shown on a form as a check box.

If the check box for this field is checked (answer is yes), I want the
other
two field (name of spouse and number of males and female children) to be
shown on the form so that the user can fill in the requested info.

how can this be done?
 

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