APPLIES TO: ACCESS 2007, 2010 (UNTESTED IN EARLIER VERSIONS)
The following tutorial shows you how you can use VBA to hide a form field in an access form following the update of a text box or after the click of a button and how to make it visible again.
For this tutorial you will need to create the following fields /controls on your Access form:
A text box called “textbox1” (the box that will be updated)
A text box called “textbox2” (the box that will be hidden)
A command button Called “btnVisible”
1. Select the text box
2. In the tools section of the “Design” tab click on the item called “Property Sheet”
3. Select the “Event” tab
4. On the “After Update” property click on the little black down arrow / triangle and select “Event Procedure”
5. Click on the three dots next to the little black down arrow / triangle – this will open up the VBA window and you should see the following:
Private Sub textbox1_AfterUpdate()
End Sub
6. On a new line directly between the two lines above add the following
Me.textbox2.Visible = False
7. Go back to your form and select the command button called “btnVisible”
8. In the tools section of the “Design” tab click on the item called “Property Sheet” and select the “Event” tab
9. On the “On Click” property click on the little black down arrow / triangle and select “Event Procedure”
10. Click on the three dots next to the little black down arrow / triangle – this will open up the VBA window and you should see the following:
Private Sub btnVisible_Click()
End Sub
11. On a new line directly between the two lines above add the following
Me.textbox2.visible = True
Save your changes and test your form / report
Note: The text box will only be hidden once the user has finished entering data and clicked away from the field.
You can adapt the above option in many ways to hide or make visible any controls on your form.
If you want a field to be hidden when the form first loads do the following:
2.1 Select the field you want to be hidden when the form loads
2.2 In the tools section of the “Design” tab click on the item called “Property Sheet” and select the “Format” tab
3.2 Change the “Visible” property to “No”
Save your changes and test your form / report