how do I clear all fields in a form?

D

DatabaseNewbie

I've created a form to enter a new record into a database. The user inserts
the record by clicking a command button, which then runs a macro containing a
few queries (there are some Conditionals as well). I want to:
1) Have all of the fields of the form clear once the user presses that
button and the data is inserted.
and 2) Maybe have a "Clear" button which would clear all fields without
inserting them first.

I am not familiar with VB (which I know is the nitty gritty behind all this)
and not extremely knowledgable about Access itself. I'm sure there is some
simple fix, but I'd appreciate your help. Thank you.
 
M

Maverick

Using VB you would need to tell each control to clear itself. The more fields
there are, the more you will have to repeat the code.

If you are using a Clear button, then you would need to assign the code to
the OnClick event. The code would look something like this:

Me.YourControlName1 = Null
Me.YourControlName2 = Null

You would continue this until you have all of the controls you want cleared
completed. Of course, you need to replace "YourControlName1" with the actual
name of your control.

If using the above creates an error, you may have to do something like:

Me.YourControlName1.Undo
Me.YourControlName1 = Null
 
B

Bobort

Use a loop to go through each control on the form and set the value to Null.

Something like this:
On Error Resume Next 'In case the control doesn't have the Value property,
an error will be raised.

Dim vntControl as Variant
For Each vntControl In Me.Controls
vntControl.Value = Null
Next vntControl
 
M

Maverick

That is a much simpler method. Thanks.

Bobort said:
Use a loop to go through each control on the form and set the value to Null.

Something like this:
On Error Resume Next 'In case the control doesn't have the Value property,
an error will be raised.

Dim vntControl as Variant
For Each vntControl In Me.Controls
vntControl.Value = Null
Next vntControl
 
D

Douglas J. Steele

Unfortunately, it won't work.

Not all controls have a Value property (labels and lines don't, for
instance)

One approach is to check the type of control:

Dim vntControl as Control

For Each vntControl In Me.Controls
Select Case vntControl.ControlType
Case acTextBox, acCombBox, acListBox, acCheckBox, ...
vntControl.Value = Null
End Select
Next vntControl

Another is to put something in the Tag property of those controls which you
want to reset. (The Tag property is a free-for-all property: you can put
anything you want into it!) Let's assume you put the word Clear into it for
those controls.

Dim vntControl as Control

For Each vntControl In Me.Controls
If vntControl.Tag = "Clear" Then
vntControl.Value = Null
End If
Next vntControl

Note that you can set the property for multiple controls at once. Select
those controls you want, then look at the Properties window. Whatever
changes you make there will apply to all selected controls.

Note, too, that I changed the declaration from Dim vntControl As Variant to
Dim vntControl As Control.
 
M

Maverick

And this is why Doug is a Microsoft Access MVP and I'm just some guy trying
to help. LOL

I'd go with Doug on this one. It is very clean and efficient.
 

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