# Solved: MS Access 2007: Creating a 'Clear All Form Fields' button that actually works



## witakr (Dec 9, 2006)

Hey there folks! I had an issue with clearing fields and that was resolved but the resolution presented another problem. A problem which I have been unsuccessful in resolving on my own.

I have tried about 3 different methods using command buttons and the VBE but nothing is working.

I need 2 buttons: One to clear all form fields and one to clear all form fields then close the form.

I know how to make a button to close a form but I am unable to make it clear the fields prior to closing. I have also been unsuccessful in creating a button to simply clear the form fields.

Any help is greatly appreciated.

Jason


----------



## OBP (Mar 8, 2005)

witakr, why on earth do you want to clear the fields before closing the Form?


----------



## witakr (Dec 9, 2006)

Because I noticed that if the user types something in one of the fields and leaves another fields blank and if that blank field happens to be a required field then it throws an error when I try to close saying the record will not be saved because not all required fields were filled. I want to avoid that. Unless there is a better way to handle it.

I still need a button that only clears all field too, though


----------



## OBP (Mar 8, 2005)

In the event of a "partial" record being entered you can use the "Undo" function to clear the whole record. However if you have an Autonumber key field it will have been incremented and it will leave a gap in the numbers.
So just clearing the fields would not overcome the "required field" errror, the record has to be undone.
But how do you know that the user wants to clear the record, shouldn't they finish entering the data?


----------



## witakr (Dec 9, 2006)

I am using an UNDO button but what I am talking about occurs before the record is saved/created. For example: Textbox1, Textbox2 and Textbox3 are requiered. The user begins typing in Textbox1 then realizes he doesn't need to create the record so the user stops entering the data in the fields. Now when the user tries to close the form he gets an error message indicating that he needs to enter something into the required fields before proceeding. But the user doesn't want to add a new record now but the form wont let him close the form without entering data in all the fields. Make sense? Clear as mud?

Even if you delete the data from all fields it will still throw this same error. How do I stop that from happening?

EDIT: Oh i forgot to mention.. I'm using unique identifiers as my primary keys so i dont use autonumber at all in the db


----------



## OBP (Mar 8, 2005)

That is what the Undo command is for. It does the same as the user pressing the Esc key twice to clear out a partial reecord.
You could remove the "required" property from the table fields and replace it with a VBA check, then if the user wants to close the form when a partial entry is made you can ask them if they are sure and then close the form.


----------



## witakr (Dec 9, 2006)

I know the solution. You made me try something and when I did it fixed it.


How do I replicate the 'Esc' key with a control button?


----------



## OBP (Mar 8, 2005)

That is the Undo command.


----------



## witakr (Dec 9, 2006)

You sir are my hero. You have solved about all my issues I have been struggling with for the last few weeks. You are awesome. Thank you for your help.

I never realized that the thing I needed most was sitting right there under my nose the whole time. Thank you.


----------



## OBP (Mar 8, 2005)

My pleasure.


----------

