Results 1 to 4 of 4

Thread: Clearing ALL controls???

  1. #1
    Guest

    Question

    I've been searching through some reference material and have done a number of searches on the forum... But haven't found what I was looking for.

    I have a cmdClear button on the Form and desire to have all information inputted into the controls wiped clean, if a User happens to want to start over.

    Is there a quick, simple, clean way to clear an entire Form and associated Input Boxes???


  2. #2
    Guest
    Code:
    For Each ctl In If TypeOf ctl Is TextBox Then
    ctl.Text="" 
    End If
    Next ctl
    This will clear text for all textboxes.

  3. #3
    Guest
    Matthew ~

    Thanks for the head's up!!! :-)

    I was reading up on the Text = "" earlier this afternoon. On my form though, there are also OptionButtons and Input Boxes requesting Yes and No answers with the Values being held by Variables.

    The end result will be the information is be dumped into a Database. If a User wants to clear all inputted data, do I need to write code for all Controls and Input Boxes seperately??? Like you did for the Text Boxes??? Or am I missing something simple like possibly a Form.Clear type of thing??? Is there anything like this???

    I could be searching for code which doesn't exist!

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    292
    found this code at:
    http://www.vb-world.net/articles/functions3/index2.html

    It requires two parameters - the form you want to clear, plus a pSkipThese string. The code checks pSkipThese and skips over any controls beginning with that string. So if you pass in 'txt' as pSkipThese, it skips all the text boxes (providing the form developer followed standard naming conventions!). Try it!

    Public Sub ClearForm(ByRef pForm As Object, _
    Optional ByVal pSkipThese As String = "")

    Dim Ctl As Control
    Dim pSkipLen As Long

    On Error Resume Next

    pSkipLen = Len(pSkipThese)

    For Each Ctl In pForm
    If pSkipThese <> "" Then
    If Left(Ctl.name, pSkipLen) <> pSkipThese Then
    Select Case TypeName(Ctl)
    Case "TextBox"
    Ctl.Text = ""
    Case "CheckBox"
    Ctl.Value = vbUnchecked
    Case "ComboBox"
    Ctl.ListIndex = -1
    Ctl.Text = ""
    End Select
    End If
    Else
    Select Case TypeName(Ctl)
    Case "TextBox"
    Ctl.Text = ""
    Case "CheckBox"
    Ctl.Value = vbUnchecked
    Case "ComboBox"
    Ctl.ListIndex = -1
    Ctl.Text = ""
    End Select
    End If
    Next

    End Sub
    "People who think they know everything are a great annoyance to those of us who do."

Posting Permissions

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



Click Here to Expand Forum to Full Width