Clear all Textboxes on Form in VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
 

This code will clear all textbox controls on the form. The second code near the bottom of the post will clear all control but bypassing certain ones.

'Visual Basic 6.0

Public Sub ClearAllText(ByVal frm As Form, ByVal ctl As Control)

  For Each ctl In frm

    If TypeOf ctl Is TextBox Then

      ctl.Text = ""

    End If

  Next

End Sub

Updated! 02/07/2008: A request was made on how to clear or manipulate all controls on a form but to bypass/skip/passby certain controls named a certain name.

Clear All Textbox Controls except those with Specific Names

'

'Setup a Control object.

Dim ctl As Control

'

'Go through each control the form has.

For Each ctl In Me.Controls

'

'If the control type if a Textbox control, then…

  If TypeOf ctl Is TextBox And Not ctl.Name = "txt2" Then

    '

    'Change the text of the controls to "" (Nothing, Blank)

    ctl.Text = “”

  EndIf

Next

Revisited: 2015

8 thoughts on “Clear all Textboxes on Form in VB 6.0

  1. Vaibhav

    Above code is really fantastic but kindly help for writing code to give common text input to all text boxes in form.
    Eg: If 10 textbox are there, out of 2 are filled by user and others are blank. So how to give text input to remaining 8 textbox by using typeof.

    Regards,

    Vaibhav

    Reply
  2. Jason Post author

    Is this what you wanted??

    I went ahead and added code to the “Clear All Textbox” Control post which will clear all text except those you specify to leave alone. 🙂

    Let me know if I didn’t understand your post properly.

    Jason

    Reply
  3. arvie

    hi guys….i really need your help…what if i have 11 textboxes and 1 textbox is empty and if i click a
    save command button there’s a message box to appear..

    please help me need some code for that..

    i really appreciate your help

    thanks and god bless!

    Reply
  4. Nafiseh

    Sorry , but it doesn’t work , because when i use this code, the ctl object does not have a text property , would you please help me in this ???

    Reply
  5. anthont tandoc

    good day sir,
    may i asked you some thing?
    i dont understand how to use to code of CLEAR.
    can u help me sir?

    Reply
  6. Jason Post author

    Clear as in set the text to nothing/blank. No text in the textbox control. Remove all text from the textbox edit box. Ect…

    Jason

    Reply
  7. Raiden

    Thank you. Even though I found a way of clearing textboxes using TextBox1.Clear() it makes a very long code. Thank you for shortening it. 😀

    Reply

Leave a Reply