Share via


How to Hide textbox based on the value from another textbox?

Question

Thursday, May 4, 2017 2:56 PM

I want to hide textbox2 based on the value from the textbox. For example: If the input in textbox1 is "Gerente" then textbox2 should hide or lock. Thank you!

All replies (5)

Thursday, May 4, 2017 4:03 PM

You could use the AfterUpdate event of the textbox1 control:

Private Sub textbox1_Afterupdate()

If Me.textbox1 = "Gerente" Then
   Me.textbox2.Visible = False
Else
   Me.textbox2.Visible = True
End if

End Sub


Thursday, May 4, 2017 4:07 PM

Also, if you wanted to lock text2 instead of hide it:

Private Sub textbox1_AfterUpdate()

If Me.textbox1 = "Gerente" Then
   Me.textbox2.Locked = True
Else
   Me.textbox2.Locked = False
End If

End Sub


Thursday, May 4, 2017 4:37 PM | 1 vote

That's not really going to do the whole job since when you move to a new record, textbox2 will still be set to its previous visibility setting. You'll also need to set the visible attribute to its default in the Form_Current event so it resets each time you access a new record.


Thursday, May 4, 2017 4:41 PM | 1 vote

Just to be clear, you need to have the same code in both the control's After Update event and the form's Current event.

The After Update event will update the visibility based on any changes made to the active record.
The Current event will control the visibility based when the record is loaded (when you navigate to a record)

Both are needed.

Daniel Pineault, 2010-2016 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net


Thursday, May 4, 2017 6:42 PM

That is true if textbox1 and textbox2 were bound to data fields. If that is the case then yes you would need to use the form_current event otherwise it wouldn't be necessary and the visibility would be correct even as you navigated through the records.