473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to get all records to .TextBody line from ADODB.Recordset /CDO.Message

20
It is necessary to send e-mail from Access 2007 using CDO.Message
and in the TextBody get/add all results/records of recordset

Part of the code as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn1_SendDA_Click()
  2.  
  3. On Error GoTo btn1_SendDA_Click_Error
  4.  
  5.  Dim Rst As ADODB.Recordset
  6.  Dim strSQL As String
  7.  
  8.  Set Rst = New ADODB.Recordset
  9.  
  10.  
  11.     strSQL = "select * from [qryDA-REQUEST]" 'source of recordset 
  12.     Rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
  13.  
  14. Do While Not Rst.EOF
  15. Set objMessage = CreateObject("CDO.Message")
  16. objMessage.Subject = "Test Message"
  17. objMessage.From = """SENDER"" <sendermail@hotmail.comm>"
  18. objMessage.To = "receivermail@gmail.com"
  19. objMessage.TextBody = ????? '(all records from Rst)
  20.  
  21. '........ extracted SMTP server configuration
  22.  
  23. objMessage.Send
  24. Rst.MoveNext
  25. Loop
  26.  
  27. Rst.Close
  28.  
  29. Set Rst = Nothing
  30. .......
  31.  
Oct 22 '12 #1
16 3841
Rabbit
12,516 Expert Mod 8TB
Use the append operator to append to the textbody as you loop through the recordset.
Expand|Select|Wrap|Line Numbers
  1. objMessage.TextBody = objMessage.TextBody & Rst("FieldName")
You should also move the rest of the message stuff outside the loop so it doesn't send an e-mail for each record.
Oct 22 '12 #2
CostasM
20
Rabbit, thanks, I tried but unsuccesfully. The source for my recordset is usual Access query, where I have:
1) e-mail address of Customer in order to send message
2) some fields with info for Customer to send

Now I reached composing of separate e-mail message for each RECORD, but I need separate message for each CUSTOMER with all records, where his e-mail address is the same.
like this:
Expand|Select|Wrap|Line Numbers
  1. objMessage.To = "receivermail@gmail.com"
  2. objMessage.TextBody =
  3. "record 1: Field 1, Field 2, Field 3, etc. 
  4.  record 2: Field 1, Field 2, Field 3, etc.
  5.  record 3: etc"
  6. 'for each above record we have the same e-mail address in a Field 3
  7.  
Probably I need to use two recordsets, 1st for e-mail addresses and 2nd - for records to group it together for each Customer. Or two While-Loop cycles ?

What do you mean "rest of the message stuff" ?
Oct 22 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
In your implementation in your original post, you are sending an email once for each record in your recordset. Anything not related to looping through the recordset should be outside the recordset.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn1_SendDA_Click()
  2.  
  3. On Error GoTo btn1_SendDA_Click_Error
  4.  
  5.  Dim Rst As ADODB.Recordset
  6.  Dim strSQL As String
  7.  
  8.  Set Rst = New ADODB.Recordset
  9.  
  10.  
  11.     strSQL = "select * from [qryDA-REQUEST]" 'source of recordset 
  12.     Rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
  13.  
  14.  
  15. Set objMessage = CreateObject("CDO.Message")
  16. objMessage.Subject = "Test Message"
  17. objMessage.From = """SENDER"" <sendermail@hotmail.comm>"
  18. objMessage.To = "receivermail@gmail.com"
  19.  
  20.  
  21. Do While Not Rst.EOF
  22.   objMessage.TextBody = objMessage.TextBody & rst("FieldName")
  23.   Rst.MoveNext
  24. Loop
  25. '........ extracted SMTP server configuration
  26.  
  27. objMessage.Send
  28.  
  29.  
  30. Rst.Close
  31.  
  32. Set Rst = Nothing
  33. .......
  34.  
Oct 22 '12 #4
CostasM
20
@TheSmileyCoder
Great! Now it works, I can send ALL records from recordset, to e-mail address, specified in "objMessage.To",
But How to send only few records, where e-mail address is the same. In my previous post I tried to explain what I really need.

To open additional recordset with a filter for e-mail addresses or to loop "objMessage.To" line ?

I need to group all records by the Recepient (e-mail) with the same e-mail address, then compose the message using this records specific for each Recepient i.e. to each Recepient to send definite records filtered by his e-mail address.

Many thanks for help
Oct 22 '12 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Well the best solution would depend a bit on your query, and what it is sorted by.

If you use the same email across several records, it sounds like your data is not properly normalized, but without your query I can't know for sure.

A quick note about your SQL:
Expand|Select|Wrap|Line Numbers
  1. strSQL="select * from [qryDA-REQUEST]" 'source of recordset
It can be simplified to just
Expand|Select|Wrap|Line Numbers
  1. strSQL="[qryDA-REQUEST]" 'source of recordset
For simplicity lets assume your query sorts by email address first, and then by whatever else you require. That way the same email will be written next to each other in the recordset.

I will also assume you have a field in your recordset containing the recipient Email, I will assume its called tx_Email.
The following is untested air code, so typos may occur.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn1_SendDA_Click()
  2.  
  3. On Error GoTo btn1_SendDA_Click_Error
  4.  
  5.  Dim Rst As ADODB.Recordset
  6.  Dim strSQL As String
  7.  dim strEmail as string
  8.  Set Rst = New ADODB.Recordset
  9.  
  10.  
  11.     strSQL = "select * from [qryDA-REQUEST]" 'source of recordset 
  12.     Rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
  13.  
  14. do while  
  15. Set objMessage = CreateObject("CDO.Message")
  16. objMessage.Subject = "Test Message"
  17. objMessage.From = """SENDER"" <sendermail@hotmail.comm>"
  18. objMessage.To = "receivermail@gmail.com"
  19.  
  20.  
  21. Do While Not Rst.EOF
  22.     strEmail=rst!tx_Email
  23.     Set objMessage = CreateObject("CDO.Message")
  24.     objMessage.Subject="Test Message"
  25.     objMessage.From="""SENDER"" <sendermail@hotmail.comm>"
  26.     objMessage.To=rst!tx_Email
  27.  
  28.   'Add fields to email body
  29.   Do while strEmail=rst!tx_Email
  30.     objMessage.TextBody = objMessage.TextBody & rst("FieldName")
  31.     rst.MoveNext
  32.   Loop
  33.   'Send email
  34.   objMessage.send
  35.   'Clear variable for next loop
  36.   set objMessage=nothing
  37. Loop
  38.  
  39. 'Cleanup 
  40.   Rst.Close
  41.   Set Rst = Nothing
  42. .......
  43.  
Oct 23 '12 #6
CostasM
20
@TheSmileyCoder
Many thanks for assistance, some part of code changed as per your recomendation but not work in proper way.
You was right, my query includes "e-mail" field, but only for sending e-mail messages. All data stored in separate tables which are normalized.

For the best understanding I have posted new post "How to create/send e-mail using data from ADODB.Recordset" with a picture what we have and what we need and last code.



Thanks again for assistance
Oct 23 '12 #7
TheSmileyCoder
2,322 Expert Mod 2GB
but not work in proper way.
Please remember that your screen is not visible to me, and I don't have a copy of your database or program, so I only get the information you give me.
not work in proper way can mean a million things. You need to be more specific, please. Are you getting errors? Is the mails not sending, or sending ot many, or getting stuck in a infinite loop?

I don't even have the possibility of testing the code I provide, since I don't have a similar setup.


some part of code changed as per your recomendation
Which parts? If you change parts of the code, you will ALWAYS need to provide a copy of your new code, otherwise I don't even know if it was error in my code, or the way it was implemented.

Also how many messages are you sending? The "best" approach can vary a bit depending on whether you are sending 1000 mails, or 2 mails.

Sending 1000 mails, the most important thing might be efficiency, but if your sending 2 mails, the best approach might be a KISS (Keep It Simple Stupid) approach.
Oct 25 '12 #8
CostasM
20
@TheSmileyCoder
I think now picture is visible which I posted in my previous post. As to last code, I posted it also in my separate post, but it was deleted by moderator as duplicated.
Now I can send all records from recordset but only to one e-mail address, specified in "objMessage.To"

I need to send e-mails to different Companies, and every Company should receive only records where her address is corresponded. It means the first we need to make a filter for ecach Company(e-mail), then compose one or some records depends on query, then to send the message.

Last code which I have is here :

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn9_Click()
  2. '
  3. Dim rst As ADODB.Recordset
  4. Dim strSQL As String
  5. Dim strEmail As String
  6. Set rst = New ADODB.Recordset
  7. '
  8. strSQL = "[qryDA-REQUEST]"  'source of recordset
  9. rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
  10. '
  11. Do While Not rst.EOF
  12. strEmail = rst.Fields("e-mail")
  13. '
  14. Set objMessage = CreateObject("CDO.Message")
  15. objMessage.Subject = " DA REQUEST" '
  16. objMessage.From = """SENDER"" <sendermail@hotmail.com>"
  17. objMessage.To = rst.Fields("e-mail")
  18. 'objMessage.To = Trim(Rst.Fields("CompanyName")) & "<" & Trim(Rst.Fields("e-mail")) & ">"  ' as variant
  19. 'objMessage.To = "receivermail@gmail.com"
  20. '
  21. '==Add fields to email body
  22. Do While strEmail = rst.Fields("e-mail")
  23.  objMessage.TextBody = objMessage.TextBody & rst(1)
  24.  rst.MoveNext
  25. Loop
  26. '========= SMTP server configuration extracted
  27. 'Send email
  28. objMessage.Send
  29. 'Clear variable for next loop
  30. Set objMessage = Nothing
  31. Loop
  32. rst.Close
  33. Set rst = Nothing
-
After running this code I've got :

- messages were sent, but each record was sent in separate message, instead of to be sent together
- last record in recordset has not been sent
- I have Error 3021

Oct 26 '12 #9
zmbd
5,501 Expert Mod 4TB
CostasM:
Please be aware that at my work PC your pictures are not visible in the least... my IT dept. blocks all off-site file depositories and I suspect so do a majority of other companies -- they don't want us downloading a bunch of company secrets to somewhere they can't access!

So when you post an error message as you've done in the last post it would be better to state the exact message given or when you post a set of data in as in post #7, an excerpt would be in order.

thnx
Oct 28 '12 #10
TheSmileyCoder
2,322 Expert Mod 2GB
As stated, you need to have the records sorted by email address. The routine is setup to loop through the records, and if the email address is the same as the previous record, it will append the information. If the email address is different, it will send the active email, and start a new one.

For the error to go away, add between lines 24 and 25:
Expand|Select|Wrap|Line Numbers
  1.   If rst.Eof Then Exit Do
Oct 28 '12 #11
CostasM
20
@TheSmileyCoder
After adding this line
Expand|Select|Wrap|Line Numbers
  1. If rst.Eof Then Exit Do
between lines 24 and 25

folowing problems are solved:

- "last record in recordset has not been sent" - ОК,
- "I have Error 3021" - no this error appeared,

but again we still have
- each record was sent in separate message, instead of to be sent together in one message.

All my records are sorted by the date and not sorted or grouped by the e-mail address, so I think we need to group it first and then send the message. My question is how to group it first and then to collect all corresponding records together in ONE message

I changed a little bit my previous picture to show my case and what I need. Hope now the picture will be visible:

Oct 28 '12 #12
CostasM
20
See picture in previous post
Oct 28 '12 #13
TheSmileyCoder
2,322 Expert Mod 2GB
Well as I presume each email is independent of the other, simply sort by email first and then by date. If you do not want to modify the query, you can just modify the way it is opened for the recordset:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM [qryDA-REQUEST] ORDER BY [E-mail], [DateField]"  'source of recordset
Sure, we can go into more complicated ways of doing it, but at present I don't see the benefit gained.
Oct 28 '12 #14
CostasM
20
Great! Thanks to all persons involved, especially to TheSmileyCoder. Now it works, I can send 1-500 messages with many records inside by clicking only one button, but faced with another problem:
-after sending numerous messages by this automatical way my SMTP provider blocked my IP address by SPAM-filter and IP address was also reputated in barracudacentral.org as "poor"

Any ideas to solve ?
Oct 29 '12 #15
zmbd
5,501 Expert Mod 4TB
Ah... that's a seperate question for another thread :)
(hint, you'll need to get on the telephone...)
Oct 29 '12 #16
TheSmileyCoder
2,322 Expert Mod 2GB
Sending a big load of mail will probably get you flagged as a spammer. I don't really know what the correct setup is for removing said blocking, but I suggest you talk to your ISP and your SMTP provider.
Oct 29 '12 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chuck | last post by:
Hi! I have a ASP page that generates an excel report. It works fine if there are records found, however, if no records are found I would like to call the below listed function instead of getting...
6
by: Joe | last post by:
I want to add a new record to database, and then retrives the @@IDENTITY value for this ne added record, and the fields of this record. However, it has the following error on line varNewID =...
0
by: Dot Netizen | last post by:
I am having trouble passing an ADODB.Recordset from a VB6 application to a VB.Net class library using COM Interop. I am running this on XP SP2 with the .Net Framework 1.1 and MDAC 2.8 SP1. I've...
5
by: Simone | last post by:
Hello I hope you guys can help me. I am very new to ADO... I am creating a ADODB connection in a module and trying to access it from a command button in a form. Function fxEIDAssgn(plngEID As...
0
by: CFW | last post by:
I thought this was going to be easy but I'm missing something . . . I need to open an ADODB recordset using the recordset source for a list box on my for. When my form opens, the list box ADODB...
3
by: | last post by:
Hello ppl, I have snippet that works fine udner ADODB with VB6, but something wrong with it in Vb.NET. Anyone can help? Recordset1 (ADODB.Recordset) Error: Arguments are of the wrong type, are...
0
by: PCroser | last post by:
I have encountered a problem when querying a table. The query passed data into a recordset which should have resulted in many records but has returned EOF. After debugging the code the only...
1
by: sanika1507 | last post by:
Hi .Actually I want to count the number of records in a recordset. So I m using the ADODB.Recordset. I just want some one to correct me. Set Cmd = Server.CreateObject("ADODB.Recordset") ...
6
by: Oko | last post by:
I'm currently developing an MS Access Data Project (.adp) in MS Access 2002. One of the reports within the DB uses data that is Dynamic and cannot be stored on the SQL Server. To resolve this, I...
1
by: Shiller | last post by:
Experts, I keep getting the following error message when accessing a web page: ADODB.Recordset (0x800A0CC1) Item cannot be found in the collection corresponding to the requested name or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.