Which Form event? (1 Viewer)

GaleT

Registered User.
Local time
Today, 02:36
Joined
Oct 18, 2019
Messages
72
I am using a single form to add, edit and view records. When I add a new record I would like to populate the "CreatedOn" form field with the current date. I think I can use a Form event for this but I can't figure out which one. Or is there a better way of doing this?

Gale
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:36
Joined
May 21, 2018
Messages
8,463
I would use afterupate
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:36
Joined
Oct 29, 2018
Messages
21,358
I would use a Default Value... :)
 

GaleT

Registered User.
Local time
Today, 02:36
Joined
Oct 18, 2019
Messages
72
Thank you MajP and the DBguy... I was hoping there was an event that knew when a New document was being created. I think if I used the AfterUpdate event I would have to determine if a new document had been created verses an update on an existing document and that's pretty complicated to me. But for this requirement the Default Value should suffice... I should have thought of that myself... but I didn't. :)

Thanks again to both of you.

Gale
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:36
Joined
Oct 29, 2018
Messages
21,358
Thank you MajP and the DBguy... I was hoping there was an event that knew when a New document was being created. I think if I used the AfterUpdate event I would have to determine if a new document had been created verses an update on an existing document and that's pretty complicated to me. But for this requirement the Default Value should suffice... I should have thought of that myself... but I didn't. :)

Thanks again to both of you.

Gale
Hi Gale. MajP and I were happy to assist. Good luck with your project.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:36
Joined
Aug 30, 2003
Messages
36,118
I was hoping there was an event that knew when a New document was being created.

For other needs the Insert events might be appropriate, and you can always test in any event:

If Me.NewRecord Then
 

GaleT

Registered User.
Local time
Today, 02:36
Joined
Oct 18, 2019
Messages
72
Thank you pbaldy, I'll look into the insert events for future needs... "If Me.NewRecord Then " that little addition to your post really makes it useful to me... just what I needed :)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:36
Joined
Feb 19, 2002
Messages
42,981
I would use afterupate
NEVER use the Form's AfterUpdate event to modify any data in the record. This puts the form into an endless loop. Earlier versions of Access simply froze but current versions, figure out the situation and are able to break out of the loop.

Dirtying a form in the AfterUpdate event forces Access to save the record again. That runs the BeforeUpdate event as the last event prior to saving a record and then the AfterUpdate event which dirties the form again forcing Access to save and run the BeforeUpdate event and then the AfterUpdate event which dirties the record again - get the picture?

The BeforeInsert event runs once for a new record immediately after the user dirties the record. You could use this event. Personally, I just give a default value for the CreateDT field when I define the table. I generally use Now() which includes time but if you want only a date, use the Date() function.
 

GaleT

Registered User.
Local time
Today, 02:36
Joined
Oct 18, 2019
Messages
72
Thank you Pat, I understand what you are saying and I appreciate the comment. I can see I will need to do more reading before using form events, they are a little confusing. In regard to your use of the Default value in the table, I realized that earlier today but I am so inexperienced with Access that I decided to keep my tables as simple as possible with no auto-populated columns. So all calculation takes place in the one and only form. I will copy your comment into my "Access Forms" note file for future reference.

Gale
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:36
Joined
May 21, 2018
Messages
8,463
This puts the form into an endless loop
To be clear that is not correct, and that is simple to prove.
Code:
Private Sub ProposalNumber_AfterUpdate()
  Me.ProposalNumber = 5
End Sub

Private Sub ProposalNumber_BeforeUpdate(Cancel As Integer)
  MsgBox "Before"
End Sub
Further changing values in any control by code does not fire any update event. That is also easy to prove

I do agree that afterupdate was not a good choice, for this scenario. However, there is no harm in setting values in the after update.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:36
Joined
Aug 30, 2003
Messages
36,118
Pat is fully capable of defending herself but I'll point out that the original question related (it appears anyway) to form level events, not control level events (bold added):

I think I can use a Form event for this

and I suspect Pat assumed you were suggesting the form's after update event. It's certainly possible that the term "Form" was being used more generically. Code in the form's after update event changing the value of a bound field will indeed cause an endless loop, or perhaps more accurately re-dirties the form and leaves it on the record it was on. I just tested and couldn't leave a record via the navigation buttons after changing a value.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:36
Joined
May 21, 2018
Messages
8,463
Yes you are correct the OP and Pat were referring to form events, and I was thinking control events. I stand corrected.
the original question related (it appears anyway) to form level events, not control level events
 

zeroaccess

Active member
Local time
Today, 04:36
Joined
Jan 30, 2020
Messages
671
Keep in mind that if a record is edited, it will again update to Now, so it's going to act more like a "last modified".
 

Micron

AWF VIP
Local time
Today, 05:36
Joined
Oct 20, 2018
Messages
3,476
Keep in mind that if a record is edited, it will again update
Not if using default value property. It is viewable for an un-saved new record but it doesn't actually exist until the record is saved. If an existing record is edited, default value doesn't come into play.
 

zeroaccess

Active member
Local time
Today, 04:36
Joined
Jan 30, 2020
Messages
671
Not if using default value property. It is viewable for an un-saved new record but it doesn't actually exist until the record is saved. If an existing record is edited, default value doesn't come into play.
I do both. A default date for the date created (modifiable by user if desired), and a last modified field that is set before update. The latter is not editable by the user.
 

Users who are viewing this thread

Top Bottom