by Elliot McCardle (3 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (5 Votes)
I had several forms with several controls on each. I wanted to clear each control, but I wanted to make it as painless as possible. This code takes a form and iterates through each control clearing the values. Currently, it works with text boxes, combo boxes, data pickers, and masked edit controls. all you have to do is pass a form name to the procedure. It uses the for each...next loop, along with the typeof method. I hope this helps.
Inputs
for name
Public Sub ClearForm(frm As Form) 'Pass a form name to it
Dim sMask As String
For Each Control In frm.Controls
If TypeOf Control Is TextBox Or TypeOf Control Is ComboBox Then
Control.Text = "" 'Clear text
End If
If TypeOf Control Is MaskEdBox Then
With Control
sMask = .Mask 'Save the existing mask
.Mask = "" 'Clear mask
.Text = "" 'Clear text
.Mask = sMask 'Reset mask
End With
End If
If TypeOf Control Is DTPicker Then
Control.Date = Date 'Set to current date
End If
Next Control
End Sub