Clear all Textboxes of the form in vb 6

The following example clear all textbox controls on the form. There are many situation when you need to reset all fields of windows form like below picture. You can use For Each loop statement to clear all the textboxes of the form one by one.

When you click on ‘Clear all’ button then code display a message ‘Are you sure to Reset all fields?’ with yes and No button if you choose ‘Yes’, all fields will be clear.

Private Sub Command1_Click()
  Dim retval
  retval = MsgBox("Are you sure to Reset all fields?", vbYesNo)
  If retval = 6 Then
     Dim txtbox As Control
     For Each txtbox In Controls
        If TypeOf txtbox Is TextBox Then txtbox.Text = ""
        Next txtbox
  End If
End Sub

‘Controls’ is the collection of all controls exist in the form. We are using ‘TypeOf’ keyword with the If statement to determine the type of a control in the Controls collection. You can also use the above code to change the appearance such as background color of specific type of the controls.

Basically ‘Controls’ collection is not a member of the Visual Basic Collection class, it is the property of the Form object so you cannot create instances of it.