the intracacies of clearing a form

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
 
C

Chris Large

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/
 

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