Question about Form Events (and which one I should use!)

CazB

Registered User.
Local time
Today, 23:50
Joined
Jul 17, 2013
Messages
309
I have a form where, whenever a new record is created, I want a report to be emailed out.
I have some code (almost ;) ) which will email the report, but which Event should I attach the code to?
The form opens in data entry mode.
I know the 'easy' option would be to put a button on the form which a user could click once they'd fill in all the details to send out the email - but I don't want them to do that because I don't want to give the the chance of NOT sending the email, iyswim?
I've tried putting the code on the AfterInsert of the form, but it isn't triggering when I want it to.. it only seems to be firing when the form is closed. I need the record to be 'complete' before it tries to send, so it should run when the user Saves the record (there's a cmdSave button), or closes the form down (there's a cmdClose button) but I don't want it to try and send it more than once... so I can't attach it to both!
So... to which event should I attach the code?
Help, please!
TIA...
 
Use the cmdSave button.. Just send out an email after performing a Check..
Code:
Private Sub cmdSave_Click()
[COLOR=SeaGreen]    ' your normal code
    'Check for if it is a New record..[/COLOR]
    [B]If Me.NewRecord Then [/B]
[COLOR=SeaGreen]        'code to send email[/COLOR]
[B]    End If[/B]
End Sub
 
Thanks for the help - I've set it on the Save button, but also added a 'flag' field onto my record to say if it's been emailed or not.... Once it's been sent, this sets to yes.

I've then got it so when they Close, it checks to see if it's been emailed already - if it hasn't, it emails out the report... if it has, it just closes the record.

Ta :)
 
Setting a flag would have been my second choice.. But, sure is another way to tackle this.. Good job :)
 
If you want to make sure this happens, you can't use an event that the user must cause to run. You need to use an event that ACCESS forces to run because a record needs to be saved. The problem is that you want to do this only the first time a record is added and not each time it is updated. But you can't send the report until the record is saved so you have to use the Form's AfterUpdate event. However, in the AfterUpdate event, you can't tell if you just added a new record or updated an existing one. SO, I would create a public variable that I set in the BeforeUpdate event and test in the AfterUpdate event.
Code:
If Me.NewRecord Then
    bNewRec = True
Else
    bNewRec = False
End If
Code:
If bNewRec = True Then
    bNewRec = False
    DoCmd.OpenReport ....
End If
 

Users who are viewing this thread

Back
Top Bottom