As an alternative lets say that you have some code that you want to use to toggle the visibilty of text box controls on your forms. Instead of writing the code behind each command button you can write the code once as a function (Public or Private).
In this example I am going to use a Public Function so that every form in my application can use it.
First of all create a new module and put this code in it.
Code:
Public Function HideControls()
Dim ctl As Control
For Each ctl In Screen.ActiveForm.Controls
If TypeOf ctl Is TextBox Then
With ctl
.Visible = False
End With
End If
Next ctl
End Function
'*******************************************
Public Function ShowControls()
Dim ctl As Control
For Each ctl In Screen.ActiveForm.Controls
If TypeOf ctl Is TextBox Then
With ctl
.Visible = True
End With
End If
Next ctl
End Function
There are two functions there, one to hide the controls and the other to show them again.
Now when ever you want to use them you just need to put the name of the function behind your command button like this:
Code:
Private Sub cmdHide_Click()
[b]HideControls[/b]
End Sub
' or
Private Sub cmdShow_Click()
[b]ShowControls[/b]
End Sub
Writing code in functions takes a little getting used to but it is definitely worth it especially if you ever need to modify something. You can make the change in one place instead of editing the code behind all your command buttons.