Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a handle on error handling... 5

Status
Not open for further replies.

jnuus

Programmer
Jul 17, 2003
29
US
I have a field that contains an email address and a command button that will bring up a new Outlook email and autofill the receipient in the "To" block. Easy enough, no prob.

If there is no data in txtEmail, the error message pops up as expected, but immediately upon closing the msgbox, a new Outlook message comes up autofilled with the last successful email.

I'm guessing
Code:
DoCmd.StopFrigginDoingThat!
would bring a syntax error, so I'm hoping that y'all will take a look at my code and guide me toward the light. I am stymied, stumped even. Thanks in advance my VBA brothers and sisters, and peace!

Code:
Private Sub cmdSendEmail_Click()
Dim mailadd As String
Dim add As String

If IsNull(txtEmail) = True Then

Err_cmdSendEmail_Click:

MsgBox "Email address not found. To send the employee email, enter an email address and save the record.", vbOKOnly + vbExclamation, "Error"

Exit Sub

Else

mailadd = txtEmail
add = "mailto:" & mailadd
Me!cmdSendEmail.HyperlinkAddress = add

End If
End Sub
 
You must have some more code that runs after that as there's no reference to sending e-mails in there at all.

Could you post the rest of then related code for us please?

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 

Hi,

There is no other code for this event. A "mailto:" hyperlink is created and autofills a new message in the default email client.

It runs perfectly - only as long as there is an email entry in the corresponding txtEmail field.

It's when the txt field is empty that creates the problem.
 
How are ya defint . . .

Strange this! . . . After you exit the msgbox [blue]you exit the sub![/blue] . . .No access to Outlook yet!

Cleaning up the code, try this:
Code:
[blue]   Dim Msg As String, Style As Integer, Title
   
   If Trim(txtEmail & "") = "" Then
      Msg "Email address not found. " & _
          "To send the employee email, " & _
          "enter an email address and save the record."
      Style = vbExclamation + vbOKOnly
      Title = "Error"
      MsgBox Msg, Style, Title
   Else
      Me!cmdSendEmail.HyperlinkAddress = "mailto:" & txtEmail
   End If[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 

AceMan,

Thanks for the code, but my results are still the same with it. I re-read my original post and I don't think I presented this problem as clearly as I should've. Apologies.

Specifically, here are the three possibilities that the end user will encounter (#3 is my problem area):

1. An email address exists in txtEmail. User clicks cmdSendEMail and the email client creates a new message. No problems with this.

2. txtEmail is empty and user attempts to send email. If user has not clicked cmdSendEMail since opening the database, error msgbox pops up, user acknowledges, nothing else happens and life is good.

3. txtEmail is empty and user attempts to send email. If, since opening the database, the user has previously clicked cmdSendEMail for a record with an email address, then the following happens:
a. Error msgbox pops up as expected
b. User closes msgbox
c. Outlook New Message pops up w/ last used email address in "To" field

Everything seems to work as it should except for Outlook chiming in at the very end.

Thanks for your help with this.
 
defint . . .

[blue]HarleyQuinn[/blue] and I have the same suspicion, that is, some other code running! No email address causes your routine to exit after the message, as in 2 of your last post.

I think its best to set a breakpoint and single step to find out just whats going on.

Also before the button is hit the 2nd time, by chance is outlook still running? (ctrl+alt+del and check the tasks list).

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 

Seriously guys, this is the only code for this event. I am building this db from the ground up - it is not an existing product being modified. And, I just started it - so it's way in the early stages yet and has almost no code in it other than what I wrote about here and a few wizards for Next Record and that sort of stuff.

This problem has me baffled. I would be happy to send it to you so you can look at the code yourself and evaluate it if you want.

I don't like giving up, but I need to have this in operation soon, so I think I am just going to write something that disables the cmd button if there is no entry in the text field. That should solve it for now, but I would love to know why the original problem is happening.

If you ever find yourself with nothing to do, build a quick form and attempt to re-create this problem using the three scenarios above. I would be indebted if you did.

But anyway, thanks for your help. PEACE!
 
Once you have set the hyperlink you need to clear it

use
Me!Command2.HyperlinkAddress = ""

below the msgbox line



ck1999
 
Try this
Code:
Private Sub cmdSendEmail_Click()
Dim mailadd As String
Dim add As String

If IsNull(txtEmail) = True Then

Err_cmdSendEmail_Click:

MsgBox "Email address not found. To send the employee email, enter an email address and save the record.", vbOKOnly + vbExclamation, "Error"
[COLOR=blue]Me!cmdSendEmail.HyperlinkAddress = ""[/color]
Exit Sub

Else

mailadd = txtEmail
add = "mailto:" & mailadd
Me!cmdSendEmail.HyperlinkAddress = add

End If

End Sub
 
defint said:
[blue]If you ever find yourself with nothing to do, build a quick form and attempt to re-create this problem using the three scenarios above. I would be indebted if you did.[/blue]
Already did so when I first read this thread. No problem in access 2K!

I asked if you would [blue]single step[/blue] thru the code to find out whats happening!

I also asked you check the [blue]Task List[/blue] before you hit the button a 2nd time.

[blue]Are these things too much too ask?[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
ck1999 & CaptainD . . .

I don't believe your seeing the problem here.

After the codes mesgbox is displayed, the next instruction is [blue]Exit Sub[/blue]. So when the user clicks ok on the msgbox the routine should exit! [purple]The hyperlink line should never get executed![/purple]

What [blue]defint[/blue] is indicating is [purple]as if the code continues on[/purple] (instead of exiting) and executes the hyperlink line! . . .

I hope this is clear . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
The code exits what I beleive is happening is you are assigning a value to the hyperlink address the 1st time you click on the button. Since this is now a property of the command button when the command button is clicked again it still retains this property and therefore if firing this action after the commandbutton_click procedure is complete. Thhe commandbutton_click procedure does exit properly but it still fires the hyperlink which is still listed in the properties. So by removing this value in the hyperlink property it not longer triggers this action.

I actually created a form and verified this before my initial post My form acted just like

1. An email address exists in txtEmail. User clicks cmdSendEMail and the email client creates a new message. No problems with this.

2. txtEmail is empty and user attempts to send email. If user has not clicked cmdSendEMail since opening the database, error msgbox pops up, user acknowledges, nothing else happens and life is good.

3. txtEmail is empty and user attempts to send email. If, since opening the database, the user has previously clicked cmdSendEMail for a record with an email address, then the following happens:
a. Error msgbox pops up as expected
b. User closes msgbox
c. Outlook New Message pops up w/ last used email address in "To" field

ck1999
 
After the codes mesgbox is displayed, the next instruction is Exit Sub. So when the user clicks ok on the msgbox the routine should exit! The hyperlink line should never get executed!

Yes, but when you run the code with an E-mail address it sets the property to that address. Then the next time you exit the sub because it is "Blank" the property is still there and the E-mail is created.
 
ck1999 . . .

[blue]Bravo![/blue] . . . don't know why I didn't see that!

Certainly worth a pinky! [thumbsup2] & CaptainD as well!

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
AceMan, I did step through the code as you suggested and it hung on the msgbox then opened up Outlook when I left the debugger. I also made sure that Outlook was not running in every instance before activating the cmd control. Problem still persisted. This db is in Access 2K.

Here's the kicker - [blue]ck1999 & CaptainD's line of code worked flawlessly![/blue] Here is the sub I am using (part of which came from you - thanks):

Code:
Private Sub cmdSendEmail_Click()
   
   Dim Msg As String
   Dim Style As Integer
   Dim Title
      
   If Trim(txtEmail & "") = "" Then
      Msg = "Email address not found. " & _
          "To send the employee email, " & _
          "enter an email address and save the record."
      Style = vbExclamation + vbOKOnly
      Title = "Error"
      MsgBox Msg, Style, Title
      Me!cmdSendEmail.HyperlinkAddress = ""
   Else
      Me!cmdSendEmail.HyperlinkAddress = "mailto:" & txtEmail
   End If

End Sub

I really appreciate all of your help on this one - Thanks AceMan, ck1999 and CaptainD. Hope to chat with you again in the future sometime.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top