in trying to clear all the input boxes on my form..
i have a textbox and combobox.. and i have found the code for clearing all textboxes which is:

For Each Control In Me.Controls
If TypeName(Control) = "TextBox" Then
Control.text = ""
End If
Next

the only thing to do is to clear all my comboboxes. by following the syntax but it doesn't work.

For Each Control In Me.Controls
If TypeName(Control) = "comboBox" Then
Control.text = "" 'or control.clear
End If
Next

Recommended Answers

All 12 Replies

Not very familiar with VB, but comboboxes would probably not have a text attribute. From this I'd say try that clear again but with a capital C or setting control.ListIndex to -1, assuming you want to clear selection and not contents.

i tried yours.. but still no luck. i continues to play without error but when it still doesn't clear the combo box whenever i clicked the command button~

it works now.. haha "combobox" is case sensitive.. it should be "ComboBox"

control.clear
or
control.ListIndex = -1

works~

another question (if i may)
i want to make it a function or sub but whenever i apply the code.. it says that me.control is invalid.. i can't specify what form because i will ve using the command on many forms. so any solution? thanks

You can do it by making a sub procedure.
The code structure should be like

Public sub ClearControls(ByVal FName as Form)

'Do your jobs here
End Sub

case1
module:

Sub cleartext()
Dim ctrl As Control
For Each ctrl In Controls
   If TypeOf ctrl Is TextBox Then
      ctrl.Text = ""
    ElseIf TypeOf ctrl Is ComboBox Then
      ctrl.Clear
   End If
Next
End Sub

form:

Private Sub cmdOk_Click()
cleartext
End Sub

error:
object required

----------------------------------------------------------

case2
module:

Public Sub cleartext(ByVal FName As Form)
Dim ctrl As Control
    For Each ctrl In Controls
       If TypeOf ctrl Is TextBox Then
          ctrl.Text = ""
        ElseIf TypeOf ctrl Is ComboBox Then
          ctrl.Clear
       End If
    Next
End Sub

form:

Private Sub cmdOk_Click()
    cleartext
    End Sub

error:
compile error: argument not optional

----------------------------------
case3:
module:

Public Sub cleartext()
Dim ctrl As Control
    For Each ctrl In Form1.Controls
       If TypeOf ctrl Is TextBox Then
          ctrl.Text = ""
        ElseIf TypeOf ctrl Is ComboBox Then
          ctrl.Clear
       End If
    Next
End Sub

form:

  Private Sub cmdOk_Click()
        cleartext
        End Sub

no error.. but then again i need to use the code in very form. sorry for the trouble .

Your 2nd & 3rd case is quite correct, but need a simple modification.

Public Sub cleartext(ByVal frm as Form)

Dim ctrl As Control
    For Each ctrl In frm.Controls
       If TypeOf ctrl Is TextBox Then
          ctrl.Text = ""
        ElseIf TypeOf ctrl Is ComboBox Then
          ctrl.Clear
       End If
    Next
End Sub

and call it

  Private Sub cmdOk_Click()
        Dim xf As Form1
        cleartext(xf)
        End Sub

Hope i can solve your probs.

still getting errors~ let's put the procedure on hold for a while. maybe we can figure it out later.

now i have a new idea. the form needs to check if all textboxes and combo boxes have a value .
if empty it tells which box is empty ..
if all boxes have a value then clear the boxes and go to the next form.

but there's something missing in my code.

Dim ctrl As Control
    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then
        If ctrl.Text = "" Then
        MsgBox ctrl.Name + "is empty"
        End If
    ElseIf TypeOf ctrl Is ComboBox Then
        If ctrl.Text = "" Then
        MsgBox ctrl.Name + "is empty"
        End If
    End If
    Next

    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then
    ctrl.Text = ""
    ElseIf TypeOf ctrl Is ComboBox Then
    ctrl.clear
    End If
    Next
    form1.hide
    Form2.Show

what this code does is it check if box is empty
if the box is empty it tells what box is empty then
clear the boxes and go to the next form

still getting errors~ let's put the procedure on hold for a while. maybe we can figure it out later.

now i have a new idea. the conditions are:
-the form needs to check if all textboxes and combo boxes have a value .
-if empty it tells which box is empty ..
-if all boxes have a value then clear the boxes and go to the next form.

but there's something missing in my code.

Dim ctrl As Control
    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then
        If ctrl.Text = "" Then
        MsgBox ctrl.Name + "is empty"
        End If
    ElseIf TypeOf ctrl Is ComboBox Then
        If ctrl.Text = "" Then
        MsgBox ctrl.Name + "is empty"
        End If
    End If
    Next

    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then
    ctrl.Text = ""
    ElseIf TypeOf ctrl Is ComboBox Then
    ctrl.clear
    End If
    Next
    form1.hide
    Form2.Show

what this code do:
-it check if box is empty
-if the box is empty it tells what box is empty then
clear the boxes and go to the next form

From my point of view, you are getting the exception from here

ElseIf TypeOf ctrl Is ComboBox Then
ctrl.clear

If you like to clear all items from combobox, it should be

ElseIf TypeOf ctrl Is ComboBox Then
    ctrl.Items.Clear()

or if you like to clear only the text, it should be

 ElseIf TypeOf ctrl Is ComboBox Then
        ctrl.SelectedIndex=-1

i have a textbox and combo box on my form now.. and i need to check if all the box have a value .. if each of them has a value then proceedto next form. if one box is empty it shows a message

bumping for update!~
i finally came up with a code with my problem.
since i'm dealing with database, when using adodc.recordset.addnew, it automatically clear all the data bounded box.

then i used a simple condition to check whether to continue saving the info if all the boxes are filled.

Private Sub cmdOk_Click()
Dim ctrl As Control
For Each ctrl In Controls
   If TypeOf ctrl Is TextBox Then
      If ctrl.Text = "" Then
      MsgBox ctrl.Tag
      Else
      [condition1]
      End If
    ElseIf TypeOf ctrl Is ComboBox Then
      If ctrl.Text = "" Then
      MsgBox ctrl.Tag
      Else
      [condition2]
      End If
   End If
Next


If [condition1 and condition2 is correct] then
frmReg.Hide
frmQuestion.Show
frmQuestion.cmdStart.Visible = True
'adodc1.recordset.update
'adodc1.refresh
End If
End Sub

on on anopther commandbutton

Private Sub cmdCancel_Click()
    For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Then
    ctrl.Text = ""
    ElseIf TypeOf ctrl Is ComboBox Then
    ctrl.Clear
    End If
    Next
frmSelect.Show
frmReg.Hide

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.