Conditional Visibility

G

Guest

OK, I started searching this board with many questions, and I was able to
find the answers to all but one.

I wanted to made a hidden field visible based on the selection of a option
field. Shifthours visibility is set to No. Here is the code I used:

Private Sub Form_Current()
If Shifthours.Value = 4 Then
OtherExplain.Visible = True
End If
If Shifthours.Value < 4 Then
OtherExplain.Visible = False
End If
If Me.Shifthours.Value = Null Then
Me.OtherExplain.Visible = False
End If
End Sub

Private Sub Frame97_AfterUpdate()
If Me![Shifthours].Value = 4 Then
Me![OtherExplain].Visible = True
End If
If Me![Shifthours].Value < 4 Then
Me![OtherExplain].Visible = False
End If
End Sub

I realize my syntax may not be the simplest, but anyway I was able to get
the field to become visible when Option 4 was selected, and disappear if the
user changed to one of the other choices. Also, I was able to use the Current
code to show the field if user moves to a record where option 4 is selected,
and hide it if it option 4 was not selected.
The problem, is, however, that when the user creates a new record, it
apparently does not use the Current code. If the user moves from a record
where Option 4 was selected, the OtherExplain box remains visible. Any ideas?
 
G

Guest

Evan,

You may need to include the code in the Before Insert event. Notice some
syntax changes I made. I also changed the logic to be a little more simple
because Me.OtherExplain is only visible if 4 is chose, so no other checks are
necessary. Also, at the end of my post you will see the use of the immediate
if (IIF) statement. I use that is situations lilke this because it reduces 4
lines of code to 1 and is very easy to read.

I would try making these changes to the events you already have defined and
if it still does not work for a new record, put a copy in the Before Insert
event of the form. Be aware that the Before Insert event will not fire until
the user types the first character in a field.

Let me know how it works out.

Private Sub Form_Current()
If Me.Shifthours = 4 Then
Me.OtherExplain.Visible = True
Else
Me.OtherExplain.Visible = False
End If
End Sub

Private Sub Frame97_AfterUpdate()
If Me.Shifthours = 4 Then
Me.OtherExplain.Visible = True
Else
Me.OtherExplain.Visible = False
End If
End Sub

The short way:

Me.OtherExplain.Visible = IIf(Me.Shifthours = 4, True, False)
 
M

Marshall Barton

The Current event works fine, your problem with a new record
is that your test for Null is invalid (Null is never equal
to anything, not even Null).

All that logic can be reduced to:

Me.OtherExplain.Visible = (Nz(Me.Shifthours, 0) = 4)
 
G

Guest

Thanks so much to both of you! Your tip did the trick, so long as i kept my
after_update code in, and replaced the Current code with yours. I tried both
codes in the Current spot, and the both were successful. Thanks much, and is
there any functional difference between the two I should know? (considering
it's working, no need to respond if there isn't)
- Evan


Klatuu said:
Evan,

You may need to include the code in the Before Insert event. Notice some
syntax changes I made. I also changed the logic to be a little more simple
because Me.OtherExplain is only visible if 4 is chose, so no other checks are
necessary. Also, at the end of my post you will see the use of the immediate
if (IIF) statement. I use that is situations lilke this because it reduces 4
lines of code to 1 and is very easy to read.

I would try making these changes to the events you already have defined and
if it still does not work for a new record, put a copy in the Before Insert
event of the form. Be aware that the Before Insert event will not fire until
the user types the first character in a field.

Let me know how it works out.

Private Sub Form_Current()
If Me.Shifthours = 4 Then
Me.OtherExplain.Visible = True
Else
Me.OtherExplain.Visible = False
End If
End Sub

Private Sub Frame97_AfterUpdate()
If Me.Shifthours = 4 Then
Me.OtherExplain.Visible = True
Else
Me.OtherExplain.Visible = False
End If
End Sub

The short way:

Me.OtherExplain.Visible = IIf(Me.Shifthours = 4, True, False)


Evan Goldin said:
OK, I started searching this board with many questions, and I was able to
find the answers to all but one.

I wanted to made a hidden field visible based on the selection of a option
field. Shifthours visibility is set to No. Here is the code I used:

Private Sub Form_Current()
If Shifthours.Value = 4 Then
OtherExplain.Visible = True
End If
If Shifthours.Value < 4 Then
OtherExplain.Visible = False
End If
If Me.Shifthours.Value = Null Then
Me.OtherExplain.Visible = False
End If
End Sub

Private Sub Frame97_AfterUpdate()
If Me![Shifthours].Value = 4 Then
Me![OtherExplain].Visible = True
End If
If Me![Shifthours].Value < 4 Then
Me![OtherExplain].Visible = False
End If
End Sub

I realize my syntax may not be the simplest, but anyway I was able to get
the field to become visible when Option 4 was selected, and disappear if the
user changed to one of the other choices. Also, I was able to use the Current
code to show the field if user moves to a record where option 4 is selected,
and hide it if it option 4 was not selected.
The problem, is, however, that when the user creates a new record, it
apparently does not use the Current code. If the user moves from a record
where Option 4 was selected, the OtherExplain box remains visible. Any ideas?
 
M

Marshall Barton

A miniscule performace difference, but no functional
difference that I can see. Just a question of how terse you
want the code to be.
--
Marsh
MVP [MS Access]


Evan said:
Thanks so much to both of you! Your tip did the trick, so long as i kept my
after_update code in, and replaced the Current code with yours. I tried both
codes in the Current spot, and the both were successful. Thanks much, and is
there any functional difference between the two I should know? (considering
it's working, no need to respond if there isn't)


Klatuu said:
You may need to include the code in the Before Insert event. Notice some
syntax changes I made. I also changed the logic to be a little more simple
because Me.OtherExplain is only visible if 4 is chose, so no other checks are
necessary. Also, at the end of my post you will see the use of the immediate
if (IIF) statement. I use that is situations lilke this because it reduces 4
lines of code to 1 and is very easy to read.

I would try making these changes to the events you already have defined and
if it still does not work for a new record, put a copy in the Before Insert
event of the form. Be aware that the Before Insert event will not fire until
the user types the first character in a field.

Let me know how it works out.

Private Sub Form_Current()
If Me.Shifthours = 4 Then
Me.OtherExplain.Visible = True
Else
Me.OtherExplain.Visible = False
End If
End Sub

Private Sub Frame97_AfterUpdate()
If Me.Shifthours = 4 Then
Me.OtherExplain.Visible = True
Else
Me.OtherExplain.Visible = False
End If
End Sub

The short way:

Me.OtherExplain.Visible = IIf(Me.Shifthours = 4, True, False)


Evan Goldin said:
OK, I started searching this board with many questions, and I was able to
find the answers to all but one.

I wanted to made a hidden field visible based on the selection of a option
field. Shifthours visibility is set to No. Here is the code I used:

Private Sub Form_Current()
If Shifthours.Value = 4 Then
OtherExplain.Visible = True
End If
If Shifthours.Value < 4 Then
OtherExplain.Visible = False
End If
If Me.Shifthours.Value = Null Then
Me.OtherExplain.Visible = False
End If
End Sub

Private Sub Frame97_AfterUpdate()
If Me![Shifthours].Value = 4 Then
Me![OtherExplain].Visible = True
End If
If Me![Shifthours].Value < 4 Then
Me![OtherExplain].Visible = False
End If
End Sub

I realize my syntax may not be the simplest, but anyway I was able to get
the field to become visible when Option 4 was selected, and disappear if the
user changed to one of the other choices. Also, I was able to use the Current
code to show the field if user moves to a record where option 4 is selected,
and hide it if it option 4 was not selected.
The problem, is, however, that when the user creates a new record, it
apparently does not use the Current code. If the user moves from a record
where Option 4 was selected, the OtherExplain box remains visible. Any ideas?
 

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