New record created to table if nothing done?? (1 Viewer)

T. McConnell

Registered User.
Local time
Yesterday, 23:53
Joined
Jun 21, 2019
Messages
63
So basically I am running into an issue where I have a form to track expenses. When the form loads, instead of having New in my Expense ID field as being a new record, it loads up to the new Auto Number already generated. Issue I have is when the form loads, it creates the record in the table, but no information may be entered at that time. If this happens over and over from the user, then it could end up having a huge amount of empty records in my Expense table.
Is there something I am missing that could be causing the form to auto generate the Expense ID on loading instead of showing New and not saving the record to the table on loading?

I have tried using the following code on the On Open as well as the On Load Event.
Code:
DoCmd.GoToRecord , , acNewRec
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 23:53
Joined
Feb 19, 2002
Messages
43,275
You have code somewhere that is dirtying the form. That is what is causing the autonumber to be generated. You also have no code in the FORM's BeforeUpdate event to validate the record and prevent bad data from being saved.
 

T. McConnell

Registered User.
Local time
Yesterday, 23:53
Joined
Jun 21, 2019
Messages
63
Thanks Pat,
I found I had some code in my OnLoad that was actually causing it ugh lol.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:53
Joined
Oct 29, 2018
Messages
21,473
Thanks Pat,
I found I had some code in my OnLoad that was actually causing it ugh lol.
If you want to make sure a certain value is entered for new records, you could consider using the Default Value property instead of just assigning the value right away.
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 23:53
Joined
Feb 19, 2002
Messages
43,275
If you want to assign a value that comes from some other place, use the Form's BeforeInsert event. It runs ONLY for new records and it runs only once, immediately after the user types the first character in ANY control. This means that the user s always the first person to dirty a record. Your code will never dirty it first.

Additionally, as I already said, it is important to put validation code in the Form's BeforeUpdate event to ensure data makes sense. If some field is required, checking for it here will allow you to provide the user with a more customized error message than Access will generate. If any dates are being entered, ALWAYS validate them for sanity. People make typos all the time. 11/4/219 is a perfectly valid date. It is also almost certainly a typo. So, at a minimum do a sanity check to ensure dates are within a reasonable time frame for their use. i.e. DOB can't be in the future. DOB for employees must ensure that the employee is between 16 and 80. You can allow people older than 80 with an override. We're all working longer but certainly 110 is excessive to still be in the workforce.
 

Users who are viewing this thread

Top Bottom