Connecting Tech Pros Worldwide Forums | Help | Site Map

Hyperlinks, input masks, and sending email in Access 2007

Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#1: Oct 21 '09
I've been trying to figure this out for three weeks. I've been sleeping on it. Not getting answers, just losing sleep. ;-)

In a table, I have a field COMPANY_EMAIL. In this field I want to allow up to 8 characters, use the same domain always, and in a form be able to click and open an email to this customer. I have tried this in numerous ways, and I think it's occuring to me finally I can't do everything in one place. I think I might need a button.

I'm using <aaaaaaaa!"@domain"."com" in my input mask. This is pretty good, except if the address is only 4 letters, it leaves 4 spaces on the left side. I don't like that. Ideas?

I have put a button next to the email field in the form (as opposed to trying to use a hyperlink and an input mask...that got me nowhere fast). Right now the button is just sitting there. I know I'll need some code probably behind "on click" but I don't know what to put there to open an email to the address specified. Ideas??

I'm aware of the mailto: part...I tried to use one field as a hyperlink with the mailto: and domain.com all in the input mask (to end up with mailto:xxx@domain.com) but sometimes, say for instance if the address was mbo@domain.com, the "m" would replace the "m" in mailto: and the "bo" would go in as the email address. When I chose an address that would go in correctly and clicked it in the form, it would never open an email anyway...so I'm trying to come up with a new plan...like the button plan above.

I need your guidance/ideas, so throw some at me!!!
Thank you so much for your time, patience, and brains! :-)
best answer - posted by ADezii
The following code will:
  1. Perform minimal validation on the E-Mail Address as entered into the [COMPANY_EMAIL] Field.
  2. Open a Session of Microsoft Outlook with the chosen E-Mail Address as entered into the [COMPANY_EMAIL] Address Field as the To: segment.
  3. Leave the Subject and Body empty for the User to fill in.
  4. Simply fill in the missing info then Send.
  5. Be sure to set a Reference to the Microsoft Outlook XX.X Object Library before you attempt to execute this code.
Expand|Select|Wrap|Line Numbers
  1. 'YOU MUST FIRST SET A REFERENCE TO THE OUTLOOK XX.X OBJECT LIBRARY
  2. Dim oLook As Object
  3. Dim oMail As Object
  4. Dim olns As Outlook.Namespace
  5. Dim strEMailAddress As String
  6. Dim txtEMail As TextBox
  7.  
  8. Set txtEMail = Me![COMPANY_EMAIL]
  9.  
  10. 'Let's at least perform Minimal Validation
  11. If IsNull(txtEMail) Then
  12.   MsgBox "No E-Mail Address listed", vbCritical, "Missing E-Mail Address"
  13.     txtEMail.SetFocus
  14.       Exit Sub
  15. ElseIf InStr(txtEMail, "@") = 0 Or InStr(txtEMail, ".") = 0 Then
  16.   MsgBox "Invalid E-Mail Address", vbCritical, "Invalid E-Mail Address"
  17.     txtEMail.SetFocus
  18.       Exit Sub
  19. End If
  20.  
  21. strEMailAddress = txtEMail
  22.  
  23. DoCmd.Hourglass True
  24.  
  25. Set oLook = CreateObject("Outlook.Application")
  26. Set olns = oLook.GetNamespace("MAPI")
  27. Set oMail = oLook.CreateItem(0)
  28.  
  29. With oMail
  30.   .To = strEMailAddress
  31.   .Body = ""
  32.   .Subject = ""
  33.     .Display
  34. End With
  35.  
  36. Set oMail = Nothing
  37. Set oLook = Nothing
  38.  
  39. DoCmd.Hourglass False

NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,765
#2: Oct 21 '09

re: Hyperlinks, input masks, and sending email in Access 2007


I'm not too hot on mailing from an application. Whenever I tried it the side issues put me off. There are a whole bunch of threads (some in which I'm involved) which deal with security problems etc etc.

Having said that, I can certainly help with the interface and how to make up, and even store, the email addresses.

Let's start with a question. Do you really want the "@blah blah.com" bit to be entered by the operator and/or saved? I would consider including that as static on the form in a Label control and just dealing with the name part in the interface.
Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#3: Oct 22 '09

re: Hyperlinks, input masks, and sending email in Access 2007


I could definetely be convinced to go one way or the other. You see, I am just doing what I can comprehend on Access...I'm not yet familiar with all the other, better ways things can be done. I'm learning A LOT here on BYTES. In fact, BYTES has probably made this project a success instead of a failure for me.
So, it seems you encourage me to just deal with the first part of the email address. I can go that route, I'll just need some guidance on how to get the @domain.com connected. The people that will be using the DB are far less computer savvy than I am, so I wanted to make it easy for them to open an email to the customer. Persuade me in a direction...your advice so far has been phenominal. :-)
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,226
#4: Oct 22 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Quote:

Originally Posted by DanicaDear View Post

I've been trying to figure this out for three weeks. I've been sleeping on it. Not getting answers, just losing sleep. ;-)

In a table, I have a field COMPANY_EMAIL. In this field I want to allow up to 8 characters, use the same domain always, and in a form be able to click and open an email to this customer. I have tried this in numerous ways, and I think it's occuring to me finally I can't do everything in one place. I think I might need a button.

I'm using <aaaaaaaa!"@domain"."com" in my input mask. This is pretty good, except if the address is only 4 letters, it leaves 4 spaces on the left side. I don't like that. Ideas?

I have put a button next to the email field in the form (as opposed to trying to use a hyperlink and an input mask...that got me nowhere fast). Right now the button is just sitting there. I know I'll need some code probably behind "on click" but I don't know what to put there to open an email to the address specified. Ideas??

I'm aware of the mailto: part...I tried to use one field as a hyperlink with the mailto: and domain.com all in the input mask (to end up with mailto:xxx@domain.com) but sometimes, say for instance if the address was mbo@domain.com, the "m" would replace the "m" in mailto: and the "bo" would go in as the email address. When I chose an address that would go in correctly and clicked it in the form, it would never open an email anyway...so I'm trying to come up with a new plan...like the button plan above.

I need your guidance/ideas, so throw some at me!!!
Thank you so much for your time, patience, and brains! :-)

Danica, what is your E-Mail Client (Outlook, Internet Explorer, etc.)?
Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#5: Oct 22 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Quote:
Danica, what is your E-Mail Client (Outlook, Internet Explorer, etc.)?
I use MS Outlook. Thanks.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,226
#6: Oct 22 '09

re: Hyperlinks, input masks, and sending email in Access 2007


It should be a relatively simple matter to automate the process of send an E-Mail to a specific Address within Outlook, but first:
  1. Where are you obtaining the E-Mail Address from? If it is a Field on a Form, then what is the Field Name (txtEMailAddress, txtCompany, etc.)?
  2. Do you want the E-Mail automatically sent with Default Body Text and Subject, or do you simply want the Outlook Window open with the To: and Subject: automatically filled in, and leave the Body: to you?, etc...
Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#7: Oct 22 '09

re: Hyperlinks, input masks, and sending email in Access 2007


1. Field COMPANY_EMAIL in table HOTSTICK_CUSTOMERS and form HOTSTICK_CUSTOMERS. (I was educated on better naming after I named everything...will do better in the future.) Also note, I changed the name to appear as "Email" to the user. But I think the program writer is mostly concerned with the name that shows in design view...which is COMPANY_EMAIL.
2. I want to open an email and leave both the subject and the body as the user's responsibility. I can see where I might eventually in the future want to automate things much more but for now I'm just a beginner and I could spend a year writing this DB if I don't watch it. ;-) (Why am I suddenly laughing out loud?)

The lab freqeuntly sends out saved emails where sometimes it would likely be easier to copy and paste the email address from Access to Outlook. On other occasions, they might want to send a unique email and that is where clicking the email address in the Access program would be more helpful. That's why I initially tried to create one text box with the domain attached and make it a hyperlink...so I could have flexibility to copy or click. I'm still open and conviceable. You people know lots more than me. I tend to take good advice. Thanks again, Danica
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,226
#8: Oct 22 '09

re: Hyperlinks, input masks, and sending email in Access 2007


The following code will:
  1. Perform minimal validation on the E-Mail Address as entered into the [COMPANY_EMAIL] Field.
  2. Open a Session of Microsoft Outlook with the chosen E-Mail Address as entered into the [COMPANY_EMAIL] Address Field as the To: segment.
  3. Leave the Subject and Body empty for the User to fill in.
  4. Simply fill in the missing info then Send.
  5. Be sure to set a Reference to the Microsoft Outlook XX.X Object Library before you attempt to execute this code.
Expand|Select|Wrap|Line Numbers
  1. 'YOU MUST FIRST SET A REFERENCE TO THE OUTLOOK XX.X OBJECT LIBRARY
  2. Dim oLook As Object
  3. Dim oMail As Object
  4. Dim olns As Outlook.Namespace
  5. Dim strEMailAddress As String
  6. Dim txtEMail As TextBox
  7.  
  8. Set txtEMail = Me![COMPANY_EMAIL]
  9.  
  10. 'Let's at least perform Minimal Validation
  11. If IsNull(txtEMail) Then
  12.   MsgBox "No E-Mail Address listed", vbCritical, "Missing E-Mail Address"
  13.     txtEMail.SetFocus
  14.       Exit Sub
  15. ElseIf InStr(txtEMail, "@") = 0 Or InStr(txtEMail, ".") = 0 Then
  16.   MsgBox "Invalid E-Mail Address", vbCritical, "Invalid E-Mail Address"
  17.     txtEMail.SetFocus
  18.       Exit Sub
  19. End If
  20.  
  21. strEMailAddress = txtEMail
  22.  
  23. DoCmd.Hourglass True
  24.  
  25. Set oLook = CreateObject("Outlook.Application")
  26. Set olns = oLook.GetNamespace("MAPI")
  27. Set oMail = oLook.CreateItem(0)
  28.  
  29. With oMail
  30.   .To = strEMailAddress
  31.   .Body = ""
  32.   .Subject = ""
  33.     .Display
  34. End With
  35.  
  36. Set oMail = Nothing
  37. Set oLook = Nothing
  38.  
  39. DoCmd.Hourglass False
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,765
#9: Oct 23 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Quote:

Originally Posted by DanicaDear View Post

I could definetely be convinced to go one way or the other. You see, I am just doing what I can comprehend on Access...I'm not yet familiar with all the other, better ways things can be done. I'm learning A LOT here on BYTES. In fact, BYTES has probably made this project a success instead of a failure for me.
So, it seems you encourage me to just deal with the first part of the email address. I can go that route, I'll just need some guidance on how to get the @domain.com connected. The people that will be using the DB are far less computer savvy than I am, so I wanted to make it easy for them to open an email to the customer. Persuade me in a direction...your advice so far has been phenominal. :-)

OK. For the sake of this we'll assume that the domain part of the email address is to be @Domain.com. We can also work on an eight character name part if you like, but I expect you can handle that in the design of your form (and control). You would need to change what type of data is expected in [COMPANY_EMAIL], as well as add a Label control after it which displayed the domain name. The Label control (Let's call it lblDomain) would display @Domain.com. This is easily changed in Design View for a different domain of course.

What I'm suggesting here is easily incorporated into ADezii's code. It would simply replace line #8.
Expand|Select|Wrap|Line Numbers
  1. Set txtEMail = Me![COMPANY_EMAIL]
All we do really is add the domain address (conveniently found in lblDomain) to the end of [COMPANY_EMAIL] to produce the full address :
Expand|Select|Wrap|Line Numbers
  1. Set txtEMail = Me.[COMPANY_EMAIL] & Me.lblDomain.Caption
Otherwise, I'm sure ADezii's code will work fine for you (not that it wouldn't without this suggestion you understand).
Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#10: Oct 23 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Thanks everyone. I'm off til Monday. Will get restarted then. I'm soooo tempted to try this right away (because I will admit this project is kind of exciting). However, 40 hours of Access is enough in a week so I definitely need to take my breaks so I don't get burnt out. (Plus I have the cutest little one year old!) :-)
Y'all have a nice weekend.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,765
#11: Oct 23 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Sounds like fun Danica. Go and enjoy your weekend (and your family). We'll all be here again on Monday I expect ;)
Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#12: Oct 26 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Hey GUYS! LOL.
I'm trying to get the email thing to work. When I tried ADezii's code with my email address I got a warning box that said "Not a valid email address." Then I tried NeoPa's suggestion and I get the debugger which takes me to NeoPa's line #8. I think I did all the suggestions you made NeoPa...but something isn't right. (Set the reference to Outlook, properly named the form, etc etc). Can you help? Thanks!
Attached Files
File Type: zip SHOPS102209b.zip (2.19 MB, 3 views)
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,186
#13: Oct 26 '09

re: Hyperlinks, input masks, and sending email in Access 2007


ADezii's code can not produce that message. How much have you changed it?
Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#14: Oct 26 '09

re: Hyperlinks, input masks, and sending email in Access 2007


On the HOTSTICK_CUSTOMERS form, all I did was add the label "@southernco.com", change the name and picture on the button, attach ADezii's code at the OnClick event, and change the line number 8 from
Expand|Select|Wrap|Line Numbers
  1. Set txtEMail = Me![COMPANY_EMAIL] 
to
Expand|Select|Wrap|Line Numbers
  1. Set txtEMail = Me.[COMPANY_EMAIL] & Me.lblDomain.Caption
If you find worse than that...I did it on accident. :-)

The form only lists 13 customers and a few of those *do* have bad email addresses...but you will be able to tell what's correct just by looking. Those that are screwed up are due to me playing with hyperlinks previously. I have only tried to use the button on the addresses that are valid.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,226
#15: Oct 26 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Quote:

Originally Posted by DanicaDear View Post

Hey GUYS! LOL.
I'm trying to get the email thing to work. When I tried ADezii's code with my email address I got a warning box that said "Not a valid email address." Then I tried NeoPa's suggestion and I get the debugger which takes me to NeoPa's line #8. I think I did all the suggestions you made NeoPa...but something isn't right. (Set the reference to Outlook, properly named the form, etc etc). Can you help? Thanks!

Completely replace your code with the following code (Copy and Paste), and all should be well in your world.
Expand|Select|Wrap|Line Numbers
  1. 'YOU MUST FIRST SET A REFERENCE TO THE OUTLOOK XX.X OBJECT LIBRARY
  2. Dim oLook As Object
  3. Dim oMail As Object
  4. Dim olns As Outlook.NameSpace
  5. Dim strEMailAddress As String
  6. Dim strEMail As String
  7.  
  8. 'Set txtEMail = Me.[COMPANY_EMAIL]
  9. strEMail = Me.[COMPANY_EMAIL] & Me.lblDomain.Caption
  10.  
  11. 'Let's at least perform Minimal Validation
  12. If IsNull(Me![COMPANY_EMAIL]) Then
  13.   MsgBox "No E-Mail Address listed", vbCritical, "Missing E-Mail Address"
  14.     Me![COMPANY_EMAIL].SetFocus
  15.       Exit Sub
  16. ElseIf InStr(strEMail, "@") = 0 Or InStr(strEMail, ".") = 0 Then
  17.   MsgBox "Invalid E-Mail Address", vbCritical, "Invalid E-Mail Address"
  18.     txtEMail.SetFocus
  19.       Exit Sub
  20. End If
  21.  
  22. DoCmd.Hourglass True
  23.  
  24. Set oLook = CreateObject("Outlook.Application")
  25. Set olns = oLook.GetNamespace("MAPI")
  26. Set oMail = oLook.CreateItem(0)
  27.  
  28. With oMail
  29.   .To = strEMail
  30.   .Body = ""
  31.   .Subject = ""
  32.     .Display
  33. End With
  34.  
  35. Set oMail = Nothing
  36. Set oLook = Nothing
  37.  
  38. DoCmd.Hourglass False
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,186
#16: Oct 26 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Ok, let's look at what the code actually does, rather than just copy and paste. The line number 8 that you changed appends the "@southernco.com" onto the [COMPANY_EMAIL], so I will assume the [COMPANY_EMAIL] you are testing with is just something like a name.
Look at lines 10 through 19. You are checking to verify that [COMPANY_EMAIL] contains both a "@" and "." symbol, and it clearly doesn't since you are going to paste them on the end later. Perhaps instead, you should verify that it doesn't have those characters.
Quote:

Originally Posted by ChipR View Post

I wrote that before ADezzi posted the new code, but finished after it, so it doesn't apply to the post right above it. Perhaps you could move/remove it?

Member
 
Join Date: Sep 2009
Location: Alabama
Posts: 101
#17: Oct 26 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Oh my goodness...you people ROCK!!!!!!!!!!!!
ChipR...thank you for your hints. They are helping.
ADezii, thanks for the code!
NeoPa, thanks for the good explanations.
I'm out of questions for now but I'm sure I'll be back with a new string very soon! LOL. (Some of you may not want to open a new can of worms. hahaha.)
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,765
#18: Oct 26 '09

re: Hyperlinks, input masks, and sending email in Access 2007


You do keep us somewhat busy Danica, but I admit to feeling pretty confident when seeing a question from you that you'll do all you can to make the question make sense and work with us when we post. That's way better than average so I'm happy to look forward to your next question.

PS. @Chip. Your points are well considered, but I think you may have overlooked something in the detail. ADezii's code checks the control contains a value, then goes on to check that the resultant variable strEMail contains both the characters "@" & ".".
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,226
#19: Oct 27 '09

re: Hyperlinks, input masks, and sending email in Access 2007


Quote:
You do keep us somewhat busy Danica, but I admit to feeling pretty confident when seeing a question from you that you'll do all you can to make the question make sense and work with us when we post. That's way better than average so I'm happy to look forward to your next question.
I'm with you 100% on this one, NeoPa.
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,186
#20: 4 Weeks Ago

re: Hyperlinks, input masks, and sending email in Access 2007


I wrote that before ADezzi posted the new code, but finished after it, so it doesn't apply to the post right above it. Perhaps you could move/remove it?
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,765
#21: 4 Weeks Ago

re: Hyperlinks, input masks, and sending email in Access 2007


I cannot sensibly remove it Chip (sorry) after various references to it, but I have added a quote to your last post into it to make it clear for anyone who may come across it.
Reply