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 )
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).
|
|
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 )
Dim varTrial as variant varTrial =DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'") if not isnull(varTrial) then stEmailTo = varTrial & ", " end if varTrial =null varTrial =DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'") if not isnull(varTrial) then stEmailTo = stEmailTo & varTrial & ", " end if varTrial =null varTrial =DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'") if not isnull(varTrial) then stEmailTo = stEmailTo & varTrial & ", " end if stEmailTo = left(stEmailTo,len(stEmailTo )-2)
|
|
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.
|
|
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 )
Dim stEmailTo As String Dim varTrial As String varTrial = Nz(DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") If varTrial <> "" Then stEmailTo = varTrial & ", " End If varTrial = Nz(DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") If varTrial <> "" Then stEmailTo = stEmailTo & varTrial & ", " End If varTrial = Nz(DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") If varTrial <> "" Then stEmailTo = stEmailTo & varTrial & ", " End If stEmailTo = Left(stEmailTo, Len(stEmailTo) - 2)
Also you need to double check that the separator your system uses is ',' and not ';'.
|
|
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.
|
|
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?
|
|
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.
|
|
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 )
stEmailTo = left(stEmailTo,len(stEmailTo )-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
|
|
|
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 )
stEmailTo = left(stEmailTo,len(stEmailTo )-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.
|
|
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?
|
|
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.
|
|
June 25th, 2008 07:12 PM
# 12
|
Re: Email TO: Handling Blank Fields
I found the immediate window. Sorry about that.
|
|
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.
|
|
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)
|
|
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.
|
|
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.
|
|
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 )
Dim stEmailTo As String Dim varTrial As String varTrial = Nz(DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") If Trim(varTrial) <> "" Then stEmailTo = Trim(varTrial) & ", " End If varTrial = Nz(DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") If Trim(varTrial) <> "" Then stEmailTo = stEmailTo & Trim(varTrial) & ", " End If varTrial = Nz(DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") If Trim(varTrial) <> "" Then stEmailTo = stEmailTo & Trim(varTrial) & ", " End If stEmailTo = Left(stEmailTo, Len(stEmailTo) - 2)
|
|
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.
|
|
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 )
Dim stEmailTo As String Dim varTrial As String varTrial = Nz(DLookup("[Contact Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") Debug.Print "Contact Email 1: " & varTrial If Trim(varTrial) <> "" Then stEmailTo = stEmailTo & Trim(varTrial) & ", " End If varTrial = Nz(DLookup("[Contact2 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") Debug.Print "Contact Email 2: " & varTrial If Trim(varTrial) <> "" Then stEmailTo = stEmailTo & Trim(varTrial) & ", " End If varTrial = Nz(DLookup("[Contact3 Email]", "[Company]", "[Loc No] = '" & Me![Loc No] & "'"), "") Debug.Print "Contact Email 3: " & varTrial If Trim(varTrial) <> "" Then stEmailTo = stEmailTo & Trim(varTrial) & ", " End If 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.
|
|
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.
|
|
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
|
|
June 27th, 2008 12:58 PM
# 22
|
Re: Email TO: Handling Blank Fields
Yes they appear to have run together.
|
|
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 ?
|
|
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?
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
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.
|
|
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
|
|
June 30th, 2008 06:51 PM
# 32
|
Re: Email TO: Handling Blank Fields
Thanks so much for all your help.
|
|
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 )
Sub temp() Dim db As DAO.Database Dim rs As DAO.Recordset Dim stEmailTo As String Dim strSQL As String Set db = CurrentDb strSQL = "SELECT [Contact Email], [Contact2 Email], [Contact3 Email] " & _ "FROM Company WHERE [Loc No]='" & Me![Loc No] & "'" Set rs = db.OpenRecordset(strSQL) rs.MoveLast rs.MoveFirst If rs.RecordCount = 0 Then MsgBox "There is no record for this [Loc No]", vbOKOnly Exit Sub End If If rs.RecordCount <> 1 Then MsgBox "There is more than one record for this [Loc No]", vbOKOnly Exit Sub End If If Trim(Nz(rs![Contact Email], "")) <> "" Then stEmailTo = stEmailTo & Trim(rs![Contact Email]) & "," End If If Trim(Nz(rs![Contact Email2], "")) <> "" Then stEmailTo = stEmailTo & Trim(rs![Contact Email2]) & "," End If If Trim(Nz(rs![Contact Email3], "")) <> "" Then stEmailTo = stEmailTo & Trim(rs![Contact Email3]) & "," End If stEmailTo = Left(stEmailTo, Len(stEmailTo) - 1) ' to test Debug.Print stEmailTo rs.Close Set rs = Nothing Set db = Nothing End Sub
|
|
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?
|
|
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.
|
|
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.
|
|
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
| |