Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Email TO: Handling Blank Fields

Question posted by: clloyd (Member) on June 24th, 2008 04:53 PM
I have a piece of Code that creates my TO portion of my email when I hit a button. The only problem is it can pull from 3 different fields. For some clients I have only one email address but for others I could have all 3 filled in. When I hit one with only one email address my code stops. How can I get it to just ignore the blank email fields?

Here is what I am using.

Code: ( text )
  1. stEmailTo = DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'") & "," & " " & DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'") & "," & " " & DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'")
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Denburt's Avatar
Denburt
Moderator
1,062 Posts
June 24th, 2008
05:33 PM
#2

Re: Email TO: Handling Blank Fields
Take into account this is off the cuff but should work. Although I don't know how you intend on implementing this the Dlookup function sure can slow things down I usually try to avoid using it whenever possible. Maybe you can open a query then check it to see what the values are and if they exist. that would be much quicker but similar for when you want to check a field. Let us know how it goes.

Code: ( text )
  1. Dim varTrial as variant
  2. varTrial =DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'")
  3. if not isnull(varTrial) then
  4.  stEmailTo = varTrial & ", "
  5. end if
  6. varTrial =null
  7. varTrial =DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'")
  8. if not isnull(varTrial) then
  9.  stEmailTo = stEmailTo & varTrial & ", "
  10. end if
  11. varTrial =null
  12. varTrial =DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'")
  13. if not isnull(varTrial) then
  14.  stEmailTo = stEmailTo & varTrial  & ", "
  15. end if
  16. stEmailTo = left(stEmailTo,len(stEmailTo )-2)

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 24th, 2008
07:40 PM
#3

Re: Email TO: Handling Blank Fields
I still get the message "Unknown message recipient(s) the message was not sent.

Thanks though.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 25th, 2008
09:37 AM
#4

Re: Email TO: Handling Blank Fields
You may be storing an empty string rather than null. Try this ...

Code: ( text )
  1. Dim stEmailTo As String
  2. Dim varTrial As String
  3.  
  4.     varTrial = Nz(DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  5.     If varTrial <> "" Then
  6.         stEmailTo = varTrial & ", "
  7.     End If
  8.    
  9.     varTrial = Nz(DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  10.     If varTrial <> "" Then
  11.         stEmailTo = stEmailTo & varTrial & ", "
  12.     End If
  13.  
  14.     varTrial = Nz(DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  15.     If varTrial <> "" Then
  16.      stEmailTo = stEmailTo & varTrial & ", "
  17.     End If
  18.    
  19.     stEmailTo = Left(stEmailTo, Len(stEmailTo) - 2)


Also you need to double check that the separator your system uses is ',' and not ';'.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 25th, 2008
02:26 PM
#5

Re: Email TO: Handling Blank Fields
That worked and you were correct the field is empty. This works perfectly now.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 25th, 2008
02:35 PM
#6

Re: Email TO: Handling Blank Fields
Sorry spoke to soon. The one I tested had all the field with an email address in it. I tested another one that had the first two but the third was blank and I received the error message again. Can you help?

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 25th, 2008
04:23 PM
#7

Re: Email TO: Handling Blank Fields
At the end of the code put

Debug.Print stEmailTo

and open the immediate window to see the result.

This will print out exactly what is in the string.

Look for empty white spaces, etc.

Reply
Denburt's Avatar
Denburt
Moderator
1,062 Posts
June 25th, 2008
04:52 PM
#8

Re: Email TO: Handling Blank Fields
What Mary stated was correct you need to verify that using a comma verses a semicolon between email addresses is appropriate. Then as she also stated at the very end of the code before the "end sub"

Code: ( text )
  1. stEmailTo = left(stEmailTo,len(stEmailTo )-2)
  2. debug.print  stEmailTo

Just to point this out from the help file:
Quote:
Originally Posted by Help
If no record satisfies criteria or if domain contains no records, the DLookup function returns a Null

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 25th, 2008
05:07 PM
#9

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by Denburt
What Mary stated was correct you need to verify that using a comma verses a semicolon between email addresses is appropriate. Then as she also stated at the very end of the code before the "end sub"

Code: ( text )
  1. stEmailTo = left(stEmailTo,len(stEmailTo )-2)
  2. debug.print  stEmailTo

Just to point this out from the help file:


My thinking is that the empty records are not empty. If they contain white space then the DLookup won't return a null.

Reply
Denburt's Avatar
Denburt
Moderator
1,062 Posts
June 25th, 2008
05:29 PM
#10

Re: Email TO: Handling Blank Fields
You always make such valid points Mary especially since the record he is looking for "Loc No" may be there and as such a record would be returned but the field may or may not contain any results, which could be as you said white space or returned as an empty field or a Null.

So clloyd what type of field are you storing the email addresses in?

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 25th, 2008
06:58 PM
#11

Re: Email TO: Handling Blank Fields
Keep in mind I am not an expert and am a little confused. I did the Print Debug that was mentioned but do not know what was meant by open immediate window.

I store the information as follows: Each location has up to five contact people each of which have Name, Title, Phone and Email address. It is a simple text field. For some locations we only have one contact and for others we could have five. All are stored in the same record. If I run this code on a location that has all the email fields in a record presenting an email address the code runs fine but any record that has one field empty it gives me the unknown recipient message. It is also rare and a keying error but there have been cases where fields 1 and 2 have an email address 3 does not but 4 does. Thanks for all your help.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 25th, 2008
07:12 PM
#12

Re: Email TO: Handling Blank Fields
I found the immediate window. Sorry about that.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 25th, 2008
07:20 PM
#13

Re: Email TO: Handling Blank Fields
I added the Code Debug.Print stEmailTo and opened the Immediate Window but nothing appeared in it.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 25th, 2008
08:25 PM
#14

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by clloyd
I added the Code Debug.Print stEmailTo and opened the Immediate Window but nothing appeared in it.


Did you put it after this line

stEmailTo = Left(stEmailTo, Len(stEmailTo) - 2)

Reply
Denburt's Avatar
Denburt
Moderator
1,062 Posts
June 25th, 2008
08:26 PM
#15

Re: Email TO: Handling Blank Fields
O.K. I just noticed Me![Loc No] is that a number or string? In your Dlookup you have it designated as a string but if the datatype in the table is a number then you need a simple correction to the DLookup function.

For a Number leave out the quotes.
Debug.print DLookup("[Contact Email]", "[Company]", "[Loc No] = " & Me![Loc No] )


O.K. try debugging from the top down. First make sure you have an email address in the first field at least and at least one of the other fields will be empty.

Try the following and if there is an email address in that field then it should show up in the immediate window:
Debug.print DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'")

If there is an email in this field and it doesn't print out in the immediate window then check some of the other items in that string to be sure they are valid.

(For example)
Debug.print Me![Loc No]

If this produces the desired result then check all fields and table names in your Dlookup function.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 26th, 2008
01:39 PM
#16

Re: Email TO: Handling Blank Fields
Ok I am getting somewhere. The record I chose had a entry in the first two email fields the other three did not have email addresses. The immediate window listed the first email address only not the second. As I mentioned before the code is fine if all five email fields have data it does not like finding a blank field. Something is missing in the code that will strip the remaining empty fields out of the TO section. I just can't see it. Thanks again.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 26th, 2008
03:12 PM
#17

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by clloyd
Ok I am getting somewhere. The record I chose had a entry in the first two email fields the other three did not have email addresses. The immediate window listed the first email address only not the second. As I mentioned before the code is fine if all five email fields have data it does not like finding a blank field. Something is missing in the code that will strip the remaining empty fields out of the TO section. I just can't see it. Thanks again.


You must be storing empty white space in the field even though there isn't an email address. Try adjusting the code as follows:

Code: ( text )
  1. Dim stEmailTo As String
  2. Dim varTrial As String
  3.  
  4.     varTrial = Nz(DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  5.     If Trim(varTrial) <> "" Then
  6.         stEmailTo = Trim(varTrial) & ", "
  7.     End If
  8.    
  9.     varTrial = Nz(DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  10.     If Trim(varTrial) <> "" Then
  11.         stEmailTo = stEmailTo & Trim(varTrial) & ", "
  12.     End If
  13.  
  14.     varTrial = Nz(DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  15.     If Trim(varTrial) <> "" Then
  16.      stEmailTo = stEmailTo & Trim(varTrial) & ", "
  17.     End If
  18.    
  19.     stEmailTo = Left(stEmailTo, Len(stEmailTo) - 2)

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 26th, 2008
03:28 PM
#18

Re: Email TO: Handling Blank Fields
Did not work. I went into the record and made sure that there was nothing in the fields that are blank and it still will not ignore the blank fields. Still getting unknown recipient message was not sent.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 26th, 2008
03:49 PM
#19

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by clloyd
Did not work. I went into the record and made sure that there was nothing in the fields that are blank and it still will not ignore the blank fields. Still getting unknown recipient message was not sent.



OK first put some debug statements in the code as follows:

Code: ( text )
  1. Dim stEmailTo As String
  2. Dim varTrial As String
  3.  
  4.     varTrial = Nz(DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  5.     Debug.Print "Contact Email 1: " & varTrial
  6.     If Trim(varTrial) <> "" Then
  7.         stEmailTo = stEmailTo & Trim(varTrial) & ", "
  8.     End If
  9.    
  10.     varTrial = Nz(DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  11.     Debug.Print "Contact Email 2: " & varTrial
  12.     If Trim(varTrial) <> "" Then
  13.         stEmailTo = stEmailTo & Trim(varTrial) & ", "
  14.     End If
  15.  
  16.     varTrial = Nz(DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "")
  17.     Debug.Print "Contact Email 3: " & varTrial
  18.     If Trim(varTrial) <> "" Then
  19.      stEmailTo = stEmailTo & Trim(varTrial) & ", "
  20.     End If
  21.    
  22.     stEmailTo = Left(stEmailTo, Len(stEmailTo) - 2)


This will show in the immediate window the value being found in each lookup.

Let me know what happens.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 26th, 2008
07:17 PM
#20

Re: Email TO: Handling Blank Fields
It came back in the immediate window like this:

Contact Email 1: someone1@somecompany.com
Contact Email 2: someone2@somecompany.com
Contact Email 3:

I changed the email addresses for confidentiality purposes. Notice the Contact Email 3 is a blank record.

We are really having to work for this one.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 26th, 2008
09:15 PM
#21

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by clloyd
It came back in the immediate window like this:

Contact Email 1: someone1@somecompany.com
Contact Email 2: someone2@somecompany.com
Contact Email 3:

I changed the email addresses for confidentiality purposes. Notice the Contact Email 3 is a blank record.

We are really having to work for this one.


Can I just confirm that you are getting in stmailto ...

someone1@somecompany.com

instead of ...

someone1@somecompany.com,someone2@somecompany.com

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 27th, 2008
12:58 PM
#22

Re: Email TO: Handling Blank Fields
Yes they appear to have run together.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 27th, 2008
01:35 PM
#23

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by clloyd
Yes they appear to have run together.



What do you mean they appear to have run together ?

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 27th, 2008
03:59 PM
#24

Re: Email TO: Handling Blank Fields
The record has an email address in the first two fields and the third was blank (empty). In the immediate window it showed as follows:

Contact Email 1: someone1@somecompany.com
Contact Email 2: someone2@somecompany.com
Contact Email 3:

Is that what you were asking?

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 27th, 2008
04:10 PM
#25

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by clloyd
The record has an email address in the first two fields and the third was blank (empty). In the immediate window it showed as follows:

Contact Email 1: someone1@somecompany.com
Contact Email 2: someone2@somecompany.com
Contact Email 3:

Is that what you were asking?



No what I want to know is exactly what shows in the immediate window if you run

Debug.Print stmailto

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 28th, 2008
01:58 AM
#26

Re: Email TO: Handling Blank Fields
That is what shows up except I had to change the names because of confidentiality for the names. It is Contact 1 email hard return Contact 2 hard return and a blank record. The empty bland record is what is causing the problem.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 28th, 2008
02:05 AM
#27

Re: Email TO: Handling Blank Fields
Quote:
Originally Posted by clloyd
That is what shows up except I had to change the names because of confidentiality for the names. It is Contact 1 email hard return Contact 2 hard return and a blank record. The empty bland record is what is causing the problem.



What you should be getting is

contact1@email.com,contact2@email.com

You shouldn't be getting any hard returns for stmailto. It should be just one continuous string.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 30th, 2008
01:11 PM
#28

Re: Email TO: Handling Blank Fields
My example is exactly what I received. It did have hard returns.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 30th, 2008
05:28 PM
#29

Re: Email TO: Handling Blank Fields
it sounds like there is a problem with the data entered in the database. Take a sample record and delete the contents of the contact email fields. Then manually retype the email addresses. It sounds like you have hard return characters being stored in these fields. If I'm right then when you type in the email addresses after deleting the content the record should output correctly.

Mary

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 30th, 2008
06:44 PM
#30

Re: Email TO: Handling Blank Fields
I did as you asked. There are no hard returns.

There needs to be something else in the code to handle an empty (null) record. As I have mentioned before the code works beautifully if there is data in all five fields. I only doesn't work if I hit a blank (null) field. It has something to do with a null records.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 30th, 2008
06:46 PM
#31

Re: Email TO: Handling Blank Fields
A null record shouldn't put in a hard return. OK I'm going to play around with the code a bit and get back to you.

Mary

Reply
clloyd's Avatar
clloyd
Member
46 Posts
June 30th, 2008
06:51 PM
#32

Re: Email TO: Handling Blank Fields
Thanks so much for all your help.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
June 30th, 2008
07:04 PM
#33

Re: Email TO: Handling Blank Fields
OK this shouldn't work any differently to the last one but its worth trying another way of doing it.

Make sure to add the Microsoft DAO library in the reference list and test the following code.

Code: ( text )
  1. Sub temp()
  2. Dim db As DAO.Database
  3. Dim rs As DAO.Recordset
  4. Dim stEmailTo As String
  5. Dim strSQL As String
  6.  
  7.     Set db = CurrentDb
  8.    
  9.     strSQL = "SELECT [Contact Email], [Contact2 Email], [Contact3 Email] " & _
  10.             "FROM Company WHERE [Loc No]='" & Me![Loc No] & "'"
  11.    
  12.     Set rs = db.OpenRecordset(strSQL)
  13.  
  14.     rs.MoveLast
  15.     rs.MoveFirst
  16.  
  17.     If rs.RecordCount = 0 Then
  18.         MsgBox "There is no record for this [Loc No]", vbOKOnly
  19.         Exit Sub
  20.     End If
  21.  
  22.     If rs.RecordCount <> 1 Then
  23.         MsgBox "There is more than one record for this [Loc No]", vbOKOnly
  24.         Exit Sub
  25.     End If
  26.  
  27.     If Trim(Nz(rs![Contact Email], "")) <> "" Then
  28.         stEmailTo = stEmailTo & Trim(rs![Contact Email]) & ","
  29.     End If
  30.    
  31.     If Trim(Nz(rs![Contact Email2], "")) <> "" Then
  32.         stEmailTo = stEmailTo & Trim(rs![Contact Email2]) & ","
  33.     End If
  34.  
  35.     If Trim(Nz(rs![Contact Email3], "")) <> "" Then
  36.         stEmailTo = stEmailTo & Trim(rs![Contact Email3]) & ","
  37.     End If
  38.    
  39.     stEmailTo = Left(stEmailTo, Len(stEmailTo) - 1)
  40.    
  41.     ' to test
  42.     Debug.Print stEmailTo
  43.    
  44.     rs.Close
  45.     Set rs = Nothing
  46.     Set db = Nothing
  47.    
  48. End Sub

Reply
clloyd's Avatar
clloyd
Member
46 Posts
July 1st, 2008
07:10 PM
#34

Re: Email TO: Handling Blank Fields
It game me a Compile Error-Expected End Sub and the cursor appeared before the Sub tem()

I am using this before other code I need to fill in the CC and Body text could that be the problem?

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
July 1st, 2008
07:34 PM
#35

Re: Email TO: Handling Blank Fields
What I was suggesting was putting this on the form module as a separate sub procedure for testing purposes. We can work out how to incorporate it into your code if it works.

Reply
clloyd's Avatar
clloyd
Member
46 Posts
July 2nd, 2008
07:10 PM
#36

Re: Email TO: Handling Blank Fields
I must be doing something wrong because it locked up my computer. I put the code on my module for that form but must not be calling it on click correctly. Any suggestions.

Reply
msquared's Avatar
msquared
Administrator
10,402 Posts
July 3rd, 2008
11:02 AM
#37

Re: Email TO: Handling Blank Fields
In the editor window. Highlight the code from sub temp to end sub and then just click the run button. It will run the highlighted procedure

Reply
clloyd's Avatar
clloyd
Member
46 Posts