Unhide fields and make it "required" on basis on text box update

  • Thread starter Thread starter sunilkeswani
  • Start date Start date
S

sunilkeswani

Hi

I want a specific text box "txt" to become visible and also become
mandatory to fill, if another text box "txt2" has a value of 155, 158,
or 160.

Could yo please advise how exactly i can do it? I am new to access.

Cheers!
Sunny
 
To make it mandatory, use the BeforeUpdate event of the form:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txt) Then
Select Case Me.txt2.Value
Case 115, 158, 160
Cancel = True
MsgBox "txt is required."
End Select
End If
End Sub

To make it visible, use the AfterUpdate of txt2. You also need to do this in
the Current event of the form, so it is visible/hidden when the form moves
record:
Private Sub txt2_AfterUpdate
Dim bShow As Boolean
Select Case Me.txt2.Value
Case 115, 158, 160
bShow = True
End Select
With Me.txt
If .Visible <> bShow Then
.Visible = bShow
End If
End With
End Sub
Private Sub Form_Current()
Call txt2_AfterUpdate
End Sub

You will need to add error handling, in case txt has focus when you attempt
to hide it.
 
Thanks, I tried this, but the txt field does not unhide itself. Should
I make it as invisible as default ? That is what i have it as currently
 
Presumably:
a) txt2 is the name of a text box.

b) You set the AfterUpdate property of the text box to:
[Event Procedure]

c) You then clicked the Build button to open the code window, and put the
code into that window.

If it is not working, temporarily add the line:
Stop
to the top of the procedure.
Then when you enter a value into txt2, the code should stop at this line.
You can press F8 to step through each line, and check what values are being
set, and what is happening as a result until you find what is wrong.
 
I think the .bshow is causing the error. the TXT box doesnt display and
still remains hidden
 
What value is in the text box when you run this?

When the code runs, does it stop on the Stop line (with yellow highlight)?

As you step through the code (pressing F8) what happens when it comes to the
line:
Case 115, 158, 160
Does it skip the next line?
If you pause the mouse over the txt2.Value, what value does it show?
 
Sorry to bother Allen, but the txt2 us updated on the basis of a combo
selection, should i do an afterupdate for the cbo ?
 
Yep: if you alter the value programmatically, the control's AfterUpdate does
not fire.

You can fire it with:
Call txt2_AfterUpdate
assuming the calling procedure is in the same module.
 
Why would you alter any bound control in Form_Current?

That means you are dirtying the record as soon as you enter it. Sounds like
a cause for concurrency problems.
 
So where do I insert Call txt2_AfterUpdate ? Sorry, but this may look
stupid to you. I am not getting it though
 
So where do I insert Call txt2_AfterUpdate ? Sorry, but this may look
stupid to you. I am not getting it though
 
Your 3rd reply was:
| Sorry to bother Allen, but the txt2 us updated on the basis of
| a combo selection, should i do an afterupdate for the cbo ?

Therefore I assume that you need to use:
Call txt2_AfterUpdate
in the AfterUpdate event of the combo that updates txt2.

Before you do that, make sure you have the code working, i.e. check that you
get the desired results when you update txt2 manually by typing a value into
the field.

Once you have that working, and the combo's AfterUpdate as well, you may
also need to add that line to the Current event of the form, but please take
it one step at a time. Otherwise it's too difficult to debug.
 
Thanks Allen, I am getting it if I manually type it into the txt2 box,
but no matter what I do, it is not happening any other way, if I place
it on after update of the combo, or even put it on form current.

Regards
 
Okay: if it is working when you type into the box, the code is working.

Now, is that combo itself updated programmatically?
Is it's AfterUpdate event actually firing?
Can you add this line to the event procedure:
Stop
and then press F8 to step through the code a trace what's running?
 
Hi I am sorry for all the trouble Allen, I got it. Finally realised
that the combo which I used for after update was linked to an another
combo, so I am now getting it okay.

Thank you for the help !!!
 
Back
Top