clearing the values of all the controls in a form

C

chanu

I want to clear the values in all the controls on my subform. I searched on
the net and found code for that.It is working well but i can't understand how
it works at some point in the IF clause in the SELECT CASE statement.Because
if the control is tested true against an empty string, the control value is
filled with null value. But after i enter a value in a control and click
cmdCLEAR how is it filled with null value though it is not an empty string.
Private Sub cmdCancel_Click()

Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl
'I have added the following line to suit my purpose
Me.cboNameAllowance.SetFocus

End Sub
please teach me clearly, friends
 
M

Marshall Barton

chanu said:
I want to clear the values in all the controls on my subform. I searched on
the net and found code for that.It is working well but i can't understand how
it works at some point in the IF clause in the SELECT CASE statement.Because
if the control is tested true against an empty string, the control value is
filled with null value. But after i enter a value in a control and click
cmdCLEAR how is it filled with null value though it is not an empty string.
Private Sub cmdCancel_Click()

Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl
'I have added the following line to suit my purpose
Me.cboNameAllowance.SetFocus

End Sub


That code has the kind of loop you want, but it does not
clear all the (bound,unbound??) controls. Not only does it
not do what you want, it will fail if you have a multi
select list box,

Because there can be a bunch of controls that do not have
the control source property (e.g. labels) and there may be
bound controls that you do not want to clear, it would be
better to have a way to specifically indentify the controls
to clear. I usually set the Tag property for those controls
to something like "CLEAR" and use code similar to:

Private Sub cmdCancel_Click()
Dim ctl As Control

For Each ctl In Me.Controls
If ctl.Tag = "CLEAR" Then
ctl.Value = Null
End If
Next ctl
Me.cboNameAllowance.SetFocus

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top