All we know Visual Basic 6 is the simplest ways to develop Windows application for desktop computer. In this special article we are going to learn a code poem which automatically clear the text box content with few line of code. The idea sound good, isn’t it?

Firstly lets make a Sub to clear the fields. I have two method the first one let you clear selected text box which utilize the paramArray argument and the second will clear the entire field in a form.
Open your Visual Basic 6.0 and enter the following sub routine.
Sub ClearBox for selected boxes
Sub ClearBox (ParamArray boxes() As Variant)
Dim ct As Variant
For Each ct In boxes
If TypeOf ct Is TextBox Then
ct.Text = “”
End If
Next
End Sub
This sub will clear the passed text box controls on the form, The sub utilizes the paramArray feature, so that the program can accept any number of arguments/controls. Remember that paramArray object should be type of variant.
Clear all the boxes
The second method will clear all the boxes on the form. Lets take a look at the second code.
Sub ClearBox_Auto(f As Form)
Dim ob As Object
Dim ct As Control
For Each ob In Form1.Controls
Set ct = ob
If TypeOf ct Is TextBox Then
ct.Text = “”
End If
Next
End Sub
Form’s controls property is returning the object collection and we collect each of then to ‘ob’ and then assign to a control variable and then can do the similar type of operations done in Clear Box. sub
Auto Number Format ‘Sub’
Finally I have a special format box which utilise Forms controls collection to gather controls appeared in the User Interface and apply specific number format to all the number field as follows.
Sub AutoNumberFormat(f As Form)
Dim ob As Object
Dim ct As Control
For Each ob In Form1.Controls
Set ct = ob
If TypeOf ct Is TextBox Then
If IsNumeric(ct.Text) Then
ct.Text = Format(ct, “0.00”)
End If
End If
Next
End Sub
Call the Box function
Let invoke the subroutines on the click event of command button
‘Clear selected boxes
Call ClearBox(TextBox1, TextBox2, TextBox3)
‘Clear All the boxes
Call ClearBox_Auto(Me)
‘Format numbered fields
Call AutoNumberFormat(Me)
Note:
The code is working on Microsoft Word VBA and Visual Studio 6.0 alike. For using on Word VBA please replace the Form (in argument) with UserForm
Join us on Disqus.
Like this:
Like Loading...
Related