If no e-mail address present

P

Pyrite

Hi,

I have a field in a form that is 'Contact E-Mail' and have put a command
button with the following code to open a new e-mail message addressed to the
e-mail address in the current record.

The Code.

Private Sub btnSendEmail_Click()
DoCmd.SendObject acSendNoObject, , ,
[Forms]![frmUpdateSiteName]![ContactE-Mail]
End Sub

My problem comes when there is no e-mail address (i.e. if the user presses
the button by mistake when there is no e-mail present) it displays the
debugger. Instead I would like it to display an error message to the user and
do nothing else.
 
J

John Spencer

Private Sub btnSendEmail_Click()
IF Len([Forms]![frmUpdateSiteName]![ContactE-Mail] & "") = 0 then
MSgBox "Please enter an Email Address"
ELSE
DoCmd.SendObject acSendNoObject,,,[Forms]![frmUpdateSiteName]![ContactE-Mail]
END IF
End Sub


John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 
P

Pyrite

Thanks very much for your response John, it was most helpful and works
brilliantly.
It has also gone a way toward helping me understand the IF ELSE functions. I
try not to copy and paste the code people supply me with on this site too
much as that is not learning so at the least I rewrite it and learn as I go
(hopefully).

I have come accross another problem related to this though but cannot figure
a way around it. I think it is because the way I am generating the e-mail is
as a 'send object'. After pressing the cmd button the e-mail editor opens
with the e-mail address in place, if I decide not to send this e-mail though
and hit the 'X' button to close it the debugger opens again, obviously
because the object has indeed not been sent and the code is left incomplete.
Is there a way around this?

Thanks again for your help.

John Spencer said:
Private Sub btnSendEmail_Click()
IF Len([Forms]![frmUpdateSiteName]![ContactE-Mail] & "") = 0 then
MSgBox "Please enter an Email Address"
ELSE
DoCmd.SendObject acSendNoObject,,,[Forms]![frmUpdateSiteName]![ContactE-Mail]
END IF
End Sub


John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
Hi,

I have a field in a form that is 'Contact E-Mail' and have put a command
button with the following code to open a new e-mail message addressed to the
e-mail address in the current record.

The Code.

Private Sub btnSendEmail_Click()
DoCmd.SendObject acSendNoObject, , ,
[Forms]![frmUpdateSiteName]![ContactE-Mail]
End Sub

My problem comes when there is no e-mail address (i.e. if the user presses
the button by mistake when there is no e-mail present) it displays the
debugger. Instead I would like it to display an error message to the user and
do nothing else.
 
J

John Spencer

That is probably generating an error (perhaps 2501?) which you will need to
trap with error code in your function. See below for a simple example.

If the error is not 2501, then change 2501 to the error number that is reported.

Private Sub btnSendEmail_Click()
On Error GoTo Proc_Error

IF Len([Forms]![frmUpdateSiteName]![ContactE-Mail] & "") = 0 then
MsgBox "Please enter an Email Address"
ELSE
DoCmd.SendObject acSendNoObject,,,[Forms]![frmUpdateSiteName]![ContactE-Mail]
END IF

Exit Sub 'Don't run the error code

Proc_Error: 'Handle errors here
If Err.Number = 2501 Then
'Do Nothing this is the expected error
Else 'Report error number and description.
MsgBox Err.Number & ": " & err.description,,"Error Occurred"
End If

End Sub


John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
Thanks very much for your response John, it was most helpful and works
brilliantly.
It has also gone a way toward helping me understand the IF ELSE functions. I
try not to copy and paste the code people supply me with on this site too
much as that is not learning so at the least I rewrite it and learn as I go
(hopefully).

I have come accross another problem related to this though but cannot figure
a way around it. I think it is because the way I am generating the e-mail is
as a 'send object'. After pressing the cmd button the e-mail editor opens
with the e-mail address in place, if I decide not to send this e-mail though
and hit the 'X' button to close it the debugger opens again, obviously
because the object has indeed not been sent and the code is left incomplete.
Is there a way around this?

Thanks again for your help.

John Spencer said:
Private Sub btnSendEmail_Click()
IF Len([Forms]![frmUpdateSiteName]![ContactE-Mail] & "") = 0 then
MSgBox "Please enter an Email Address"
ELSE
DoCmd.SendObject acSendNoObject,,,[Forms]![frmUpdateSiteName]![ContactE-Mail]
END IF
End Sub


John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
Hi,

I have a field in a form that is 'Contact E-Mail' and have put a command
button with the following code to open a new e-mail message addressed to the
e-mail address in the current record.

The Code.

Private Sub btnSendEmail_Click()
DoCmd.SendObject acSendNoObject, , ,
[Forms]![frmUpdateSiteName]![ContactE-Mail]
End Sub

My problem comes when there is no e-mail address (i.e. if the user presses
the button by mistake when there is no e-mail present) it displays the
debugger. Instead I would like it to display an error message to the user and
do nothing else.
 
P

Pyrite

Thank you again for your time John. Hopefully I have learnt from this again
and will know how to supress error messages in the future. The only bit I
didnt really understand was the line of code relating to the message box of
any other occuring error but I'm sure with another couple of read throughs of
that line I will figure what each bit means and does.

It was 2501 btw.

Thanks again, much appreciated.

John Spencer said:
That is probably generating an error (perhaps 2501?) which you will need to
trap with error code in your function. See below for a simple example.

If the error is not 2501, then change 2501 to the error number that is reported.

Private Sub btnSendEmail_Click()
On Error GoTo Proc_Error

IF Len([Forms]![frmUpdateSiteName]![ContactE-Mail] & "") = 0 then
MsgBox "Please enter an Email Address"
ELSE
DoCmd.SendObject acSendNoObject,,,[Forms]![frmUpdateSiteName]![ContactE-Mail]
END IF

Exit Sub 'Don't run the error code

Proc_Error: 'Handle errors here
If Err.Number = 2501 Then
'Do Nothing this is the expected error
Else 'Report error number and description.
MsgBox Err.Number & ": " & err.description,,"Error Occurred"
End If

End Sub


John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
Thanks very much for your response John, it was most helpful and works
brilliantly.
It has also gone a way toward helping me understand the IF ELSE functions. I
try not to copy and paste the code people supply me with on this site too
much as that is not learning so at the least I rewrite it and learn as I go
(hopefully).

I have come accross another problem related to this though but cannot figure
a way around it. I think it is because the way I am generating the e-mail is
as a 'send object'. After pressing the cmd button the e-mail editor opens
with the e-mail address in place, if I decide not to send this e-mail though
and hit the 'X' button to close it the debugger opens again, obviously
because the object has indeed not been sent and the code is left incomplete.
Is there a way around this?

Thanks again for your help.

John Spencer said:
Private Sub btnSendEmail_Click()
IF Len([Forms]![frmUpdateSiteName]![ContactE-Mail] & "") = 0 then
MSgBox "Please enter an Email Address"
ELSE
DoCmd.SendObject acSendNoObject,,,[Forms]![frmUpdateSiteName]![ContactE-Mail]
END IF
End Sub


John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County

Pyrite wrote:
Hi,

I have a field in a form that is 'Contact E-Mail' and have put a command
button with the following code to open a new e-mail message addressed to the
e-mail address in the current record.

The Code.

Private Sub btnSendEmail_Click()
DoCmd.SendObject acSendNoObject, , ,
[Forms]![frmUpdateSiteName]![ContactE-Mail]
End Sub

My problem comes when there is no e-mail address (i.e. if the user presses
the button by mistake when there is no e-mail present) it displays the
debugger. Instead I would like it to display an error message to the user and
do nothing else.
 

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

Top