Blank records when form closes

  • Thread starter Thread starter Bruce Hawkins
  • Start date Start date
B

Bruce Hawkins

I have a data entry form that enters data in a table, no query involved. On
the form I have a subform that shows the data from the table for the section
that is selected in a combo box on the form.

If I open the form, pick a section, the data displays, and I can add names
and data to the table ( most of the data is picked in combo boxes on the
form). Seems to work fine, but if I open the form, do nothing and then
close the form it will sometimes insert a blank record in the table
(actually a record with all the default values entered and no name).

Can anyone explain what is happening, and how I can prevent it? I have
tried some code for undo record in the close button but that just messes up
the last entry, and does not work for the open/close problem. It does not
seem to be a problem with just a simple data entry form with text boxes and
no subform.

Thanks.
Bruce Hawkins
 
This is usually caused by "Default Values" being entered into fields on
a New record. If any kind of Update or Refresh occurs before closing out
the form, that record is saved, and it appears to be a "blank record".
(actually a record with all the default values entered and no name).

Try opening a new record, and closing it right away... any blank record?
If not, then those "occasional" times you get a blank you may have done
something to trip off an Update. (clicked in the subform, or put the cursor
in some field, clicked a button, etc... etc...) Something you're doing is
causing the new record (with defaults) to Update. Check out all your
subform code...

hth
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions
 
Do you have code to set value of any bound Control on the Form in the
Form_Open / Load / Activate / Current Event or from out side this Form?

As soon a bound Control value is set, the Form's Dirty Property will become
True and Access will save this new Record when the Form is closed (unless
Undo is performed).

You can check this by running the following statement in the Immediate /
Debug just after the Form is opened:

?Forms!YourFormName.Dirty
 
Ok, most of the time I will get a blank just opening and closing the form,
but I have done it when I first open Access and not gotten the blank. The
Immediate / Debug shows the form dirty most of the time, so that is how I
have things set up. I have tried to use Undo as part of the code for the
close button, but it will mess up my last record if the form is not dirty.
When I enter a record I have an "add record" button that goes to "new
record", but the form is not dirty then, so some, but not all the fields are
deleted, when the Undo runs.
Is there a way to code this with an IIf statement (or something similar).
What I tried did not compile.
Thanks
Bruce Hawkins
 
(this is the same reply I sent to Al Camp if you read that)
Ok, most of the time I will get a blank just opening and closing the form,
but I have done it when I first open Access and not gotten the blank. The
Immediate / Debug shows the form dirty most of the time, so that is how I
have things set up. I have tried to use Undo as part of the code for the
close button, but it will mess up my last record if the form is not dirty.
When I enter a record I have an "add record" button that goes to "new
record", but the form is not dirty then, so some, but not all the fields are
deleted, when the Undo runs.
Is there a way to code this with an IIf statement (or something similar).
What I tried did not compile.
Thanks
Bruce Hawkins
 
Sorry but I don't follow your description entirely.

The Dirty Property should generally be False just after the the opening of
the Form for data entry (meaning after the Form_Current Event) UNLESS you
intentionally do so by code assigning some values to bound Controls which I
don't recommend exactly for the problem you are facing.

I would rather assign a value to the DefaultValue Property than assign a
value to the Value Property which dirties the Form unnecessarily.
 
I will chech the code again, but as far as I know the only code I use sets
default values after the controls are updated. Having said that, the form
is dirty right after I open it. Since it will probably be something deep in
the structure and hard to find, is there an easy way to code the close
button to do an undo if the form is dirty. This seems safe since the form
seems to be clean after I hit the "add record" button that moves to the next
"new record".
Thanks.
Bruce Hawkins
 
While it is possible to fudge around with undo, the *correct* method is to
find out why the Form gets dirtied as soon as it is open and eliminate the
cause of this.

Depend on the Access version you use, there is also a Form_Dirty Event.
What I would do is to add the Debug.Print statement in various Events that
occurs during the Form opening and also in the Form_Dirty Event. After
opening the Form, you can check the Innediate window and the which Event
happens just before the Form being dirtied and then trace the code
step-by-step to see which line of code actually dirties the Form and fix it.
 
So you are not going to let me take the easy way out. My problem will be
learning to use the debug tools which are beyond my limited experience, but
you have already given me a good start. Thanks for the help, and I may be
back again.
Bruce Hawkins
 
Yep ... That's the general idea.

The reason is that there may be other problems that will only surface later
and if this happens, I do you a disservice by advising you to do things I
don't normally do. Generally, I only advise people to do things a certain
way if I would do the same way in my work. And if I had your current
problem, I would try to find the cause and eliminate it rather than trying
to work around it with Undo.
 
I totally agree with Van. Most folks on the newsgroup feel the same way.
Better to find out what you coded that causes the problem, than try to
program around it. If you don't, it will come back to haunt you... perhaps
on the next form you design...
When you find the problem, I'm sure Van and I would like to know what you
found.
hth
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions
 

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

Back
Top