the intracacies of clearing a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys,

I have been trying to figure out the best practices for clearing a form that already has the record ID autokey entered (want to keep this)

I found this snippet:

Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If (Left$(ctl.ControlSource, 1) <> "=") Then
ctl.Value = ""
End If
End If
Next ctl

but, I do have some required fields and this throws an error for these fields needing to be not null.

also, what about subforms?

anybody have any good experience with this, Im sure others would appreciate it too/

Cheers,
David
 
Hi

I'd do the following:

Set the tag property of all the fields i'd like cleared to
say CLEAR (including on any subforms). Then add the
following sub:

Private Sub ClearControls(frm As Form)
Dim ctl As Control
For Each ctl In frm.Controls
If ctl.Tag = "CLEAR" Then
If ctl.DefaultValue <> "" Then
ctl = ctl.DefaultValue
Else
ctl = Null
End If
End If
Next ctl
End Sub

this can be run by the following for a main form:

Call ClearControls(Me)

or this for a subform (in a control called sfcSubForm):

Call ClearControls(Me!sfcSubForm.Form)


hth

Chris

-----Original Message-----
Hi guys,

I have been trying to figure out the best practices for
clearing a form that already has the record ID autokey
entered (want to keep this)
I found this snippet:

Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If (Left$(ctl.ControlSource, 1) <> "=") Then
ctl.Value = ""
End If
End If
Next ctl

but, I do have some required fields and this throws an
error for these fields needing to be not null.
also, what about subforms?

anybody have any good experience with this, Im sure
others would appreciate it too/
 
Back
Top