473,382 Members | 1,615 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,382 software developers and data experts.

Send 2 Queries in 1 Email with DAO recordset

This is my first post and I have a limited amount of knowledge of VBA so please bear with me, lol. I am needing to create a script that will allow me to pull from two tables and send 1 message that includes the results in two separate grids. I have the code that will allow for one table, but I don't know how to get it done for the second. Below is the working code.

Expand|Select|Wrap|Line Numbers
  1. Dim olApp As Object
  2.     Dim olItem As Variant
  3.     Dim db As DAO.Database
  4.     Dim rec As DAO.Recordset
  5.     Dim strQry As String
  6.     Dim aHead(1 To 2) As String
  7.     Dim aRow(1 To 2) As String
  8.     Dim aBody() As String
  9.     Dim lCnt As Long
  10.  
  11.     'Create the header row
  12.     aHead(1) = "Driver Name"
  13.     aHead(2) = "Expiration Date"
  14.  
  15.  
  16.     lCnt = 1
  17.     ReDim aBody(1 To lCnt)
  18.     aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th><th>") & "</th></tr>"
  19.  
  20.     'Create each body row
  21.         strQry = "SELECT * From Email_Query"
  22.     Set db = CurrentDb
  23.     Set rec = CurrentDb.OpenRecordset("CDL60DayExp")
  24.  
  25.  
  26.     If Not (rec.BOF And rec.EOF) Then
  27.         Do While Not rec.EOF
  28.             lCnt = lCnt + 1
  29.             ReDim Preserve aBody(1 To lCnt)
  30.             aRow(1) = rec("Name")
  31.             aRow(2) = rec("CdlExp")
  32.             aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>"
  33.             rec.MoveNext
  34.         Loop
  35.     End If
  36.  
  37.     aBody(lCnt) = "Hello," & "<br>" & _
  38.     "<br>" & _
  39.     "Below are the drivers with upcoming expirations" & "<br>" & _
  40.     "<br>" & _
  41.     aBody(lCnt) & "</table></body></html>"
  42.  
  43.  
  44.     'create the email
  45.     Set olApp = CreateObject("Outlook.application")
  46.     Set olItem = olApp.CreateItem(0)
  47.  
  48.     olItem.display
  49.     olItem.To = "someperson@somecompany.net"
  50.     olItem.Subject = "Upcoming Expirations"
  51.     olItem.HTMLBody = Join(aBody, vbNewLine)
  52.     olItem.display
  53.  
Apr 22 '16 #1
1 1339
ADezii
8,834 Expert 8TB
I see absolutely no easy way to accomplish this, but here goes my attempt. The idea is to:
  1. Create the Code that opens an Instance of Outlook.
  2. Dynamically create the HTML Table Code based on the first of two Recordsets and assign it to a String Variable. Assign other Variables accordingly.
  3. Dynamically create the HTML Table Code based on the second of two Recordsets and assign it to a String Variable. Assign other Variables accordingly specific to this Recordset as appropriate.
  4. Concatenate both String Variables to the HTMLBody Property of an Outlook Item.
  5. Display the Outlook Window in which two, totally independent HTML Tables are displayed each based on a different Recordset.
  6. There are a couple of small items to be worked out, one being the relocation of the Header info, but this is the basic concept.
  7. Download the Attachment to see what I mean, it is fully operational.
  8. Some Code has intentionally been omitted.
    Expand|Select|Wrap|Line Numbers
    1. '--------------------------------------------------------------------------
    2. Dim aHead2(1 To 3) As String
    3. Dim aRow2(1 To 3) As String
    4. Dim aBody2() As String
    5. Dim rec2 As DAO.Recordset
    6. Dim lCnt2 As Integer
    7.  
    8. strSQL2 = "SELECT [First Name], [Last Name],[Job Title] FROM Employees"
    9. Set rec2 = db.OpenRecordset(strSQL2, dbOpenDynaset)
    10.  
    11. 'Create the header row
    12. aHead2(1) = "First Name"
    13. aHead2(2) = "Last Name"
    14. aHead2(3) = "Job Title"
    15.  
    16. lCnt2 = 1
    17. ReDim aBody2(1 To lCnt2)
    18.  
    19. aBody2(lCnt2) = "<HTML><body><table border='2'><tr><th>" & Join(aHead2, "</th><th>") & "</th></tr>"
    20.  
    21. If Not (rec2.BOF And rec2.EOF) Then
    22.   Do While Not rec2.EOF
    23.     lCnt2 = lCnt2 + 1
    24.       ReDim Preserve aBody2(1 To lCnt2)
    25.         aRow2(1) = rec2("First Name")
    26.         aRow2(2) = rec2("Last Name")
    27.         aRow2(3) = rec2("Job Title")
    28.           aBody2(lCnt2) = "<tr><td>" & Join(aRow2, "</td><td>") & "</td></tr>"
    29.     rec2.MoveNext
    30.   Loop
    31. End If
    32.  
    33. 'aBody2(lCnt2) = "Hello," & "<br>" & _
    34. '"<br>" & _
    35. '"Here are the Employee Names that you requested" & "<br>" & _
    36. '"<br>" & _
    37. aBody2(lCnt2) & "</table></body></html>"
    38. '--------------------------------------------------------------------------
    39.  
    40. strBody1 = Join(aBody, vbNewLine)
    41. olItem.HTMLBody = strBody1 & Join(aBody2, vbNewLine)
    42. olItem.display
Attached Files
File Type: zip Test.zip (29.5 KB, 79 views)
Apr 22 '16 #2

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

Similar topics

0
by: David Burson | last post by:
Hi, I have a VB.NET windows app that needs to automatically send a simple text email when my users run a new version of the app for the first time. I thought this would be simple, but after...
1
by: StevenBarnes | last post by:
I need to have some .Net code be able to send Email using my ISP's SMTP server. I found some articles on how to use the MAIL FROM: and RCPT TO: commands that you can stream to a tcp connection on...
10
by: Mike Charney | last post by:
Is there a simple way to send SMTP email from Access VBA? Mike m charney at dunlap hospital dot org
3
by: markxxiv | last post by:
How can I send queries, reports, and perhaps forms for one database via email to a colleague who also has Access 2003 and the same database as the one I'm using to create these queries and reports? ...
9
by: Mahernoz | last post by:
Can i send an email from JavaScript? Is it possible? If yes please the code to send email using javascript...
1
by: Wells Wang | last post by:
Hi, Everybody: Sorry to bother you. I am trying to use the code below to send an email with asp.net2.0& win XP. But it failed. On my laptop I can use outlook2003 to send email. If you are...
4
Frinavale
by: Frinavale | last post by:
Introduction Many .NET applications will require an email be sent out for various reasons. This article will give a quick examples on how to send an email using VB.NET. The examples given can...
1
by: chaitanya02 | last post by:
Hi All, Well- this question might have appeared several times on this forum- but would appreciate your reply on this: I have a asp page, where customers login with some username and the pwd,...
3
by: BLUE | last post by:
I want to send an email from my web service. I do not want to act like an email client, giving credentials of an existing email account to send messages: I would like to send my mail directly to...
4
by: Tony M | last post by:
VS 2005 - XP media - VB .net - winforms - .net 2.0 Just trying to send an email, here is the code and the error message that I get. I can't figure out how to fix it?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.