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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send email to multiple names in a textbox 1

Status
Not open for further replies.

netrusher

Technical User
Feb 13, 2005
952
US
I have a textbox on my form. This texbox is populated from a Listbox. What I am trying to accomplish is the
following:

I want to tie an event to a button and when the button is clicked I would like for it to pull all the names from
the TextBox [Engineering Distribution] and have them become To: on a email ToEmail1. I think I know how
to do this with one name in the Textbox but I would like for it to add every name in the Textbox to the email.
The names are located in a Table FormEngMETbl The Full Name is in the EngME Field. The login
Name is in the [LoginName] field of the Table. If Allen Jackson is the Name in the TextBox, his login Name
would be abajacks. Each Name in the TextBox would have a LoginName. I would have to add @nmhg.com
to each LoginName. Again, I can do this with one name but do not know how to accomplish this with mulitple
names in the textbox. Can anyone help me accomplish this or does anyone have abetter idea on how to
do this?

Code:
       Dim O As Outlook.Application
       Dim m As Outlook.MailItem
       Dim toEmail1 As String
       Dim toEmail2 As String
       Dim ccEmail1 As String
       Dim ccEmail2 As String
       Dim ccEmail3 As String
       Dim bccEmail1 As String
       Dim SL As String, DL As String
       SL = vbNewLine
       DL = SL & SL

Set O = CreateObject("Outlook.Application")
Set m = Outlook.CreateItem(0)
toEmail1 = ""
ccEmail1 = "abkkulhy@nmhg.com"
ccEmail2 = "abkkulhy@nmhg.com"
ccEmail3 = ""
bccEmail1 = "abajacks@nmhg.com"

m.To = toEmail1
m.CC = ""
m.BCC = bccEmail1
m.Subject = "Form #" & Forms!ECNBCNVIPfrm![ECNBCNVIP ID].Value & "; This ECN is ready for SOURCING"
m.Body = "This ECN is Ready for SOURCING." & DL & _
       "Form # " & Forms!ECNBCNVIPfrm![ECNBCNVIP ID].Value & DL & _
       "Thank you for your help" & DL & _
       DLookup("ActualName", "ChangeFormUserNameTbl", "LoginName='" & GetCurrentUserName() & "'") & SL & _
       "ECN Analyst"

m.Display

On Error GoTo Err_CloseForm_Click


    DoCmd.Close

Exit_CloseForm_Click:
    Exit Sub

Err_CloseForm_Click:
    MsgBox Err.Description
    Resume Exit_CloseForm_Click
    
End Sub
 
Which line of code raises the error ?
I suspect the DLookUp.
Run your code step by step in debug mode to discover what happens.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Explain us what happens when you debug your code.
What is the value of x when the error raises ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You may try to replace this:
If Len(Trim(x) & "") > 0 Then
with this:
If Trim(x & "") > "" Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Phv,

It is working. When I inherited this database I did not check everything totally. I just found that the word
Login was misspelled in the table. I corrected the spelling
and it is working. I really appreciate your help!

Now I have one more question. Currently this code is picking
up the names from the Engineering Distribution field. How
can I add another field with names in it also for email. I
have another field named Quality Distribution. I would like
to pick up those names also at the same time as the
Engineering Distribution field. Can this be done?

and again thanks so very much.
 
The Table name for the Quality Engineers is FormQAEngTbl
 
PHV,

I have been fooling around and I tried the below code. I am
getting the names from the Quality Distribution textbox but
not from the Engineering Distribution Textbox.

Code:
toEmail1 = ""
Dim x
For Each x In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Engineering Distribution], vbCrLf)
  If Trim(x & "") > "" Then
    toEmail1 = toEmail1 & DLookup("LoginName", "FormEngMETbl", "EngME='" & x & "'") & "@nmhg.com;"
  End If
Next
toEmail1 = ""
Dim y
For Each y In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Quality Distribution], vbCrLf)
  If Trim(y & "") > "" Then
    toEmail1 = toEmail1 & DLookup("LoginName", "FormQAEngTbl", "QAEng='" & y & "'") & "@nmhg.com;"
  End If
Next
 
I figure it has something to do with 2 ToEmail1 but I don't
know how to change it.
 
Another possible issue is:

There might not be a name in the one or the other
TextBoxes!! Sometimes they are empty.
 
PHV,

The below code is picking up both TextBoxes but will not
work if one of them is empty. I get a invalid use of null!

Code:
Dim x
Dim y
For Each x In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Engineering Distribution], vbCrLf)
  If Trim(x & "") > "" Then
    toEmail1 = toEmail1 & DLookup("LoginName", "FormEngMETbl", "EngME='" & x & "'") & "@nmhg.com;"
  End If
Next
For Each y In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Quality Distribution], vbCrLf)
  If Trim(y & "") > "" Then
    toEmail1 = toEmail1 & DLookup("LoginName", "FormQAEngTbl", "QAEng='" & y & "'") & "@nmhg.com;"
  End If
Next
 
Code:
Dim x
Dim y
toEmail1 = ""
For Each x In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Engineering Distribution], vbCrLf)
  If Trim(x & "") > "" Then
    y = DLookup("LoginName", "FormEngMETbl", "EngME='" & x & "'")
    If Trim(y & "") > "" Then
      toEmail1 = toEmail1 & y & "@nmhg.com;"
    End If
  End If
Next
For Each x In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Quality Distribution], vbCrLf)
  If Trim(x & "") > "" Then
    y = DLookup("LoginName", "FormQAEngTbl", "QAEng='" & x & "'")
    If Trim(y & "") > "" Then
      toEmail1 = toEmail1 & y & "@nmhg.com;"
    End If
  End If
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

The code works great unless one of the textboxes are empty.
I get invalid use of null if a textbox is empty.
 
Which line of code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Phv,

Code:
For Each x In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Engineering Distribution], vbCrLf)
 
For Each x In Split(Forms!ECNBCNVIPfrm!ECNDetailfrm![Engineering Distribution][!] & ""[/!], vbCrLf)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PH,

It is working now. I suppose if later I need to add another
text box to pick up names I just extrapolate this out further?

Do I need to add a Dim Z to keep this going?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top