1 Attachment(s)
clean controls from form with error
Hi
I'm trying to clear the form using the code below
Code:
Function ClearAll(frm As Form)
Dim ctl As Control
For Each ctl In frm.Controls
Select Case TypeName(ctl) 'ctl.ControlType
Case "TextBox"
ctl.Value = ""
Case "OptionGroup", "ComboBox", "ListBox"
ctl.Value = Null
Case "Checkbox"
ctl.Value = False
'Case ac
End Select
Next
End Function
But return error Runtime error 438
Attachment 163295
error occurs because my combobox is Style = Dropdown List
Re: clean controls from form with error
Quote:
Originally Posted by
mutley
Hi
I'm trying to clear the form using the code below
Code:
Function ClearAll(frm As Form)
Dim ctl As Control
For Each ctl In frm.Controls
Select Case TypeName(ctl) 'ctl.ControlType
Case "TextBox"
ctl.Value = ""
Case "OptionGroup", "ComboBox", "ListBox"
ctl.Value = Null
Case "Checkbox"
ctl.Value = False
'Case ac
End Select
Next
End Function
But return error Runtime error 438
Attachment 163295
error occurs because my combobox is
Style = Dropdown List
Put "ComboBox" as a seperate case.
then make sure the combobox has a blank item and either set that as "" or set listindex = 0
Re: clean controls from form with error
just clear that error..
you do want all Contols blank
Code:
Option Explicit
Private Sub Command1_Click()
Call clearForm(Me)
End Sub
Public Sub clearForm(ByRef myForm As Object)
On Error GoTo EH
Dim objCtrl As Control
For Each objCtrl In myForm.Controls
If TypeOf objCtrl Is TextBox Then
objCtrl = ""
End If
If TypeOf objCtrl Is CheckBox Then
objCtrl = 0
End If
If TypeOf objCtrl Is ComboBox Then
objCtrl = ""
End If
Next
EH:
If Err.Number = 438 Then
Resume Next
End If
End Sub
Private Sub Form_Load()
Combo1.AddItem "Test"
End Sub
Re: clean controls from form with error
To clear a combo with set to drop down list set the listindex to -1