# Solved: Automatically clear form fields when opened



## witakr (Dec 9, 2006)

Hey there folks.. I have what may amount to be a small problem or an easy fix but I can't figure it out for the life of me. I have spent the last week researching this problem, off and on.

What I need help with:

I have a semi-complex database in Access 2007 and I will have several forms. I need the forms to have clear fields when the user opens the form. Is there a way to automatically clear the forms upon opening of the form by the user without the need to click a 'clear' button? If so, how do I go about achieving this end?

Thanks,
Jason


----------



## turbodante (Dec 19, 2008)

How about something like the following in the 'On Load' event...

Private Sub Form_Load()
Text6 = ""
End Sub


----------



## witakr (Dec 9, 2006)

I don't see an 'On Load' event. I tried it in code builder but it doesn't appear to be working. Am I doing something wrong?


----------



## witakr (Dec 9, 2006)

Where do I find the name of the fields to use in place of the 'Text6' in my form? The name I am thinking I need contains a space which I know wont always for in code. I think if I know where to find the name of the fields to be cleared I can get it to work.


----------



## turbodante (Dec 19, 2008)

witakr said:


> I don't see an 'On Load' event. I tried it in code builder but it doesn't appear to be working. Am I doing something wrong?


You should see 'On Load' in th property sheet, under the Event tab - in the form's Design mode.


----------



## turbodante (Dec 19, 2008)

witakr said:


> Where do I find the name of the fields to use in place of the 'Text6' in my form? The name I am thinking I need contains a space which I know wont always for in code. I think if I know where to find the name of the fields to be cleared I can get it to work.


if you select the object, you can find it's name in the property (in design mode), it should be at the top of the 'All' tab.

If the name contains a space, then just enclose it in [square brackets]


----------



## OBP (Mar 8, 2005)

Why not open the form as a new Record?


----------



## witakr (Dec 9, 2006)

This:


```
Private Sub Form_Load()
[Unit Number] = ""
End Sub
```
...is not working. The name of the field in the form is 'Unit Number'.


----------



## witakr (Dec 9, 2006)

OBP said:


> Why not open the form as a new Record?


Could you expound on this a little for me, please?


----------



## witakr (Dec 9, 2006)

I apologize for my ignorance but as I am pretty new to Access and I am not entirely familiar with the ins-and-outs yet. I have some VB experience as well as some Access so code is not entirely lost on me. I have experience with in inner workings of SQL dbs and web based applications. 

That being said, please feel free to talk to me like I am completely ignorant or some what competent behind a keyboard. I should be able to follow. Though I may have a follow up question.


----------



## turbodante (Dec 19, 2008)

witakr said:


> This:
> 
> 
> ```
> ...


 that's stumped me.


----------



## turbodante (Dec 19, 2008)

As for OBP's suggestion...

you could use

Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub

which should open your form at a new record


----------



## witakr (Dec 9, 2006)

turbodante said:


> that's stumped me.


Is there something I can provide to you which might unstump you?


----------



## turbodante (Dec 19, 2008)

witakr said:


> I apologize for my ignorance but as I am pretty new to Access and I am not entirely familiar with the ins-and-outs yet. I have some VB experience as well as some Access so code is not entirely lost on me. I have experience with in inner workings of SQL dbs and web based applications.
> 
> That being said, please feel free to talk to me like I am completely ignorant or some what competent behind a keyboard. I should be able to follow. Though I may have a follow up question.


I should have made the disclaimer too - I'm a relative newbie to access too. You'll find that OBP is the guru and this is his stomping ground.


----------



## turbodante (Dec 19, 2008)

witakr said:


> Is there something I can provide to you which might unstump you?


I think OBP's suggestion would be a good way to go, see # 12 for how.

But if you insist on clearing the fields 'manually' do expalin why, and we'll see what else we can come up with.


----------



## OBP (Mar 8, 2005)

If you are opening the form from a Switchboard or a Command Button you can open the Form on a new Record.
Or you can use the code that Turbo gave you in post #12.


----------



## witakr (Dec 9, 2006)

This:


```
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
```
...also does not work. This solution would be good for my new records form but not so much for my forms for modifying existing records. This may not be an issue as the fields can be populated with a record in the 'modify' forms but with the 'add new' forms I need the fields to be cleared automatically.


----------



## witakr (Dec 9, 2006)

turbodante said:


> I think OBP's suggestion would be a good way to go, see # 12 for how.
> 
> But if you insist on clearing the fields 'manually' do expalin why, and we'll see what else we can come up with.


This db is to be used by the most computer illiterate and ignorant people known on this green earth and I need to make it as simple as possible. Clearing the fields of a newly opened form which is used to add entirely new records is a must because if they submit the 'add new' form it will throw an error about duplication and the users head will explode along with my cell phone... I'm trying to prevent this from happening.


----------



## witakr (Dec 9, 2006)

OBP said:


> If you are opening the form from a Switchboard or a Command Button you can open the Form on a new Record.
> Or you can use the code that Turbo gave you in post #12.


Ok this is a notch over my head. What is the 'Switchboard' and 'Command Button'. I am assuming by 'command button' you are referring to the button I created for adding records but the 'switchboard' part is entirely unfamiliar to me.


----------



## witakr (Dec 9, 2006)

witakr said:


> This:
> 
> 
> ```
> ...


I am beginning to think it is something I am doing or failing to do which is causing the problem.


----------



## OBP (Mar 8, 2005)

The Command Button that you "created for adding records" is the place to put the code that Turbo posted. 
A Switchboard is a form that normally opens with the database and gives the uses lots of options for opening forms and Report etc.
When you say Turbo's code does not work, what actually happens?


----------



## witakr (Dec 9, 2006)

OBP said:


> The Command Button that you "created for adding records" is the place to put the code that Turbo posted.
> A Switchboard is a form that normally opens with the database and gives the uses lots of options for opening forms and Report etc.
> When you say Turbo's code does not work, what actually happens?


There is absolutely no change. No errors, nothing.

EDIT: I need to put the code Turbo mentioned in the button? I need this action to happen when the form is opened without clicking a button.


----------



## OBP (Mar 8, 2005)

How do you "Open" the form?


----------



## OBP (Mar 8, 2005)

This code opens a form in "add" mode

DoCmd.OpenForm "Kitchens", , , , acFormAdd

Note you need the correct number of commas.


----------



## witakr (Dec 9, 2006)

OBP said:


> How do you "Open" the form?


Using the access interface. The list on the left-hand side.


----------



## witakr (Dec 9, 2006)

OBP said:


> This code opens a form in "add" mode
> 
> DoCmd.OpenForm "Kitchens", , , , acFormAdd
> 
> Note you need the correct number of commas.


Now that you mention it I suppose it would be easier for the user to have a form to use to open other forms.


----------



## witakr (Dec 9, 2006)

Ok, OBP's idea of using a form for the user to access other forms to manage the db is a great idea and solved this specific problem. Thanks yall!


----------

