Create a Clear Form Button in Access

squeakums

Well-known Member
Joined
May 15, 2007
Messages
851
Office Version
  1. 365
I built a form in access and it autofills in other fields when something is selected. I want to create a button to clear all of the data. Is there a code I can use for this?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Whether or not the form is bound will make a difference. If bound and you're on a record, that record will have all of its editable fields wiped out. In that case you'd go to a new record, not clear the fields.
If not bound, then you can loop over text boxes and combo boxes and set them to null. However, some controls that have values cannot be set to null (option and toggle buttons are 2 types, IIRC). Something like (untested):

VBA Code:
Dim ctl As Control
For Each ctl in Me.Controls
   If ctl.Type = acTextbox Or ctl.Type = acComboBox Or... <list them here Then ctl = Null
Next
It's also common to use the tag property of the controls you want to affect. If the tag was SetNull:

VBA Code:
Dim ctl As Control
For Each ctl In Me.Controls
  If ctl.Tag = "SetNull" Then ctl = Null
Next
 
Upvote 0
Whether or not the form is bound will make a difference. If bound and you're on a record, that record will have all of its editable fields wiped out. In that case you'd go to a new record, not clear the fields.
If not bound, then you can loop over text boxes and combo boxes and set them to null. However, some controls that have values cannot be set to null (option and toggle buttons are 2 types, IIRC). Something like (untested):

VBA Code:
Dim ctl As Control
For Each ctl in Me.Controls
   If ctl.Type = acTextbox Or ctl.Type = acComboBox Or... <list them here Then ctl = Null
Next
It's also common to use the tag property of the controls you want to affect. If the tag was SetNull:

VBA Code:
Dim ctl As Control
For Each ctl In Me.Controls
  If ctl.Tag = "SetNull" Then ctl = Null
Next
I'm confused about how to write in my clear fields, the 2nd code isn't working and the 1st is giving an error. fields are named Service ID, SR ID etc.
 
Upvote 0
Post what you tried, not the pseudo code that I gave as an example, and maybe address the uncertainties I mentioned?
 
Upvote 0
Many of my fields are bound, I used the macro with a button I created but it doesn't seem to clear anything out. I was wondering if I need to include the field names to state to clear those back to null in the code like the ones I stated above?
 
Upvote 0
For bound fields it shouldn't matter if you refer to the control or the field it's bound to. For unbound controls, there is no field to refer to.
 
Upvote 0
So do I use this code below but add my field names in and if so, how would I write those in?

SQL:
Dim ctl As Control
For Each ctl In Me.Controls
  If ctl.Tag = "SetNull" Then ctl = Null
Next
 
Upvote 0
No, you do not add in the field names to that. Did you put SetNull as the Tag property in all of the controls you want to affect if you intend to use that example?
 
Upvote 0
No, you do not add in the field names to that. Did you put SetNull as the Tag property in all of the controls you want to affect if you intend to use that example?
I'm sorry but what are the directions on how to do that part.
 
Upvote 0
Look at the property sheet tabs for those controls in design view, or Google it?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top