Don't sendObject on close if record is cancelled

BarryMK

4 strings are enough
Local time
Today, 18:04
Joined
Oct 15, 2002
Messages
1,350
Hi I can't get my head around the logic on a small problem.

I have a form set up where on the onClose event an email is sent using sendObject this works fine. However I want the user to be able to cancel the entire record if they've screwed up the entry so there is a cancel Command Button using DoCmd.RunCommand acCmdDeleteRecord. If this cancel is used I don't want the email to be generated and I can't for the life of me work out the process. Help me I'm having a senior moment!
 
Can you put the email routine in the close button?
 
I suspect that the onclose event runs no matter how the form is closed, and thus the send will be triggered. What I would do is define a public variable say Sendit and on form open set it to "Dosend", in the cancel event set it to "Nosend" and in the close event test and send or don't dependent on the value.

Brian
 
I suspect that the onclose event runs no matter how the form is closed, and thus the send will be triggered. What I would do is define a public variable say Sendit and on form open set it to "Dosend", in the cancel event set it to "Nosend" and in the close event test and send or don't dependent on the value.

Brian

Kind of what I had in mind but I was tending to take it a bit deeper and set the var to 'send' only after the record is changed (IsDirty?)...
 
Morning (here anyway Ken) guys. Had to bail out early yesterday for band rehearsal (far more important than mere work with our first gig looming) so had no chance to acknowledge your replies. I sort of figured that the onClose event would fire whatever but you've confirmed that. I also wondered about using OnDirty and may look at that first.

As for the Public Variable now that would be new territory for this Access mangler but I like adding to the knowledge store so I'll do a bit of digging and have a crack at setting one up, I will probably come back to you on that one......... :o I'll let you know how I progress. Cheers and thanks for the input as always.
 
Public variable is declared in a module, I used to group them and any global code in a module called, surprise, Global :D

Public varname as string

This can then be used in all other areas eg form code and other modules.

As to which events you use that depends on your actual project.

hope rehearsal went well.

Brian
 
Thanks Brian I've learnt something new. It hurt the grey matter for a while but once I'd grasped your ideas I got it to work. Here are the details for anyone who needs them.

Code:
Module

Option Compare Database
Option Explicit
Public Sendit As String

Form declarations

Option Compare Database
Option Explicit
Dim t As Long
Dim strEmail As String
Dim Sendit As String



Private Sub Form_Open(Cancel As Integer)
	Sendit = "DoSend"
End Sub


Private Sub cmdCancel_Click()
On Error GoTo Err_cmdCancel_Click
Sendit = "NoSend"
   If MsgBox("Once YES is selected this cancel cannot be undone", vbQuestion + vbYesNo, "Are you sure you wish to cancel this entire record?") = vbYes Then
      
        DoCmd.RunCommand acCmdSelectRecord
                DoCmd.RunCommand acCmdDeleteRecord
                DoCmd.Close acForm, "frmNewRec"
        DoCmd.OpenForm "frmSwitchboard"
    End If
Exit_cmdCancel_Click:
    Exit Sub

Err_cmdCancel_Click:
    MsgBox "Record deleted"
    Resume Exit_cmdCancel_Click
    
End Sub

OnClose Event
	If Sendit = "DoSend" Then
		DoCmd.SendObject , , , strEmail, , , strSubject, strBody, True

Else
		If Sendit = "NoSend" Then
	DoCmd.CancelEvent
End If
End If
 
Happy to help, never know when I might be stuck in one of your leisure centres. :D

Brian
 

Users who are viewing this thread

Back
Top Bottom