+ Reply to Thread
Results 1 to 10 of 10

How to empty all comboboxes and textboxes in a userform

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    How to empty all comboboxes and textboxes in a userform

    Hi everyone,

    I have a userform with many comboboxes and textboxes and I am using the following code to empty those controls:

    Private Sub CommandButton2_Click()
    Beep
    If MsgBox("Are you sure you want to clear all form fields?", vbQuestion + vbYesNo, "Confirmation") = vbYes Then
    TextBox2 = ""
    TextBox3 = ""
    TextBox4 = ""
    TextBox5 = ""
    TextBox6 = ""
    TextBox7 = ""
    TextBox8 = ""
    TextBox9 = ""
    TextBox10 = ""
    TextBox11 = ""
    TextBox12 = ""
    ComboBox1 = ""
    ComboBox2 = ""
    ComboBox3 = ""
    ComboBox4 = ""
    ComboBox5 = ""
    ComboBox6 = ""
    End If
    TextBox2.SetFocus
    End Sub
    Is there another way to clear contents of all comboboxes and textboxes instead of that one in order not edit the code in case of adding new controls.

    Thanks,

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: How to empty all comboboxes and textboxes in a userform

    Maybe

    Dim Ctrl As Control
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Or TypeName(Ctrl) = "ComboBox" Then
                Ctrl.Text = ""
            End If
        Next Ctrl
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to empty all comboboxes and textboxes in a userform

    Thanks alot,

    That is what I am really looking for,,

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How to empty all comboboxes and textboxes in a userform

    You can simply close then open the userform

    Unload Me
    Load UserForm1
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to empty all comboboxes and textboxes in a userform

    What if we want to except some controls (comboboxes and textboxes) from being cleared .. what should we add to the previous code?

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How to empty all comboboxes and textboxes in a userform

    Try using the Control's Tag Property. This code will clear any control whose Tag Property is set to x
    Dim oCtrl As MSForms.Control
    For Each oCtrl In Me.Controls
             If oCtrl.Tag = "x" Then oCtrl.Text = ""
    Next oCtrl

  7. #7
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to empty all comboboxes and textboxes in a userform

    Hi everyone,

    first of all I would like to thank royUK for his useful tip about tag property, which I should have recognize it a long time ago in order to use it is such codes.

    The latest code posted by royUK is giving the error (Object does not support this property of method) and highlight this part of the code,
    oCtrl.Text = ""
    What I did to get rid of that error is combining the two codes provided by royUK within one code and get the following perfect code without errors:

    Dim Ctrl As Control
    
    For Each Ctrl In Me.Controls
    If (TypeName(Ctrl) = "TextBox" Or TypeName(Ctrl) = "ComboBox") And Not Ctrl.Tag = "x" Then Ctrl.Text = ""
    Next Ctrl
    Thanks a lot,
    Last edited by LoveCandle; 05-05-2009 at 06:28 AM.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How to empty all comboboxes and textboxes in a userform

    This might be neater
      Dim Ctrl   As msforms.Control
    
        For Each Ctrl In Me.Controls
            If Ctrl.Tag = "x" Then
                Select Case TypeName(Ctrl)
                    Case "TextBox": Ctrl.Value = ""
                    Case "ComboBox": Ctrl.ListIndex = -1
                End Select
            End If
        Next Ctrl

  9. #9
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: How to empty all comboboxes and textboxes in a userform

    your way is really neater,

    Thanks a lot,

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How to empty all comboboxes and textboxes in a userform

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1