Clearing TextBox in VB is easy, you can simply do this using code below.
//Assume that the object name of the Textbox is txtSample
txtSample.Text = ""
The code above is very reliable when you have only 1 up to 20 TextBox object but if you have 20 or more TextBox objects, this will be unworthy for your effort. To clear all TextBox objects in a form, you will need some simple steps below.
STEP 1
We need to create a function that will determine if the controls in the form is TextBox and then clear its value (see the code below).
//We named the function as clearTxt
Sub ClearText(Form As Form)
//Assign variable name to all the objects
Dim CurrentObject As Control
//Get all the objects in the form
For Each CurrentObject In Form.Controls
//Determine if the current object is Textbox
If TypeOf CurrentObject Is TextBox Then
//If yes, clear the TextBox contents
CurrentObject.Text = ""
End If
Next
End Sub
//Assign variable name to all the objects
Dim CurrentObject As Control
//Get all the objects in the form
For Each CurrentObject In Form.Controls
//Determine if the current object is Textbox
If TypeOf CurrentObject Is TextBox Then
//If yes, clear the TextBox contents
CurrentObject.Text = ""
End If
Next
End Sub
STEP 2
Now that we have the function, it's time to CALL IT! (see the code below).
//Assume that the object name of the Command Button is cmdClear
Private Sub cmdClear_Click()
Private Sub cmdClear_Click()
Call ClearText(Me)
End Sub
We're done! Now you can simply clear the TextBox content while maintaining your code lesser. Thanks for reading!