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

emailing query results in access vba

I have a database we use for order tracking. I collect information such as the vendor we purchased from as well as tracking information from each vendor for each order. Sometime the orders require multiple vendors and multiple tracking numbers. What I would like is a button to send all the tracking numbers for an order to the customers email.

this is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command9_Click()
  2.         'Send the E-Mail
  3.  
  4.         Dim oApp As Outlook.Application
  5.         Dim oMail As MailItem
  6.         Dim Subjectline As String
  7.         Dim emailaddr As String
  8.         Dim SC As String
  9.         Dim PO As String
  10.         Dim SalesC As String
  11.         PO = Forms![Orders].[PO_NUM]
  12.         SC = DLookup("SalesChannel", "orders", "PO_NUM ='" & PO & "'")
  13.         emailaddr = DLookup("Cust_EMAIL", "orders", "PO_NUM ='" & PO & "'")
  14.         If SC = "1" Then
  15.             SalesC = "DoD EMALL"
  16.         Else
  17.             If SC = "2" Then
  18.                 SalesC = "GSA Advantage"
  19.             Else
  20.             SalesC = ""
  21.             End If
  22.         End If
  23.  
  24.         Subjectline = "Tracking Inforamtion for your " & SalesC & " Order " & PO
  25.         Set oApp = CreateObject("Outlook.application")
  26.  
  27.         Dim MyBodyText As String
  28.  
  29.         MyBodyText = "QUERY RESULTS SOMEHOW"
  30.  
  31.  
  32.         Set oMail = oApp.CreateItem(olMailItem)
  33.         oMail.Body = MyBodyText
  34.         oMail.Subject = Subjectline
  35.         oMail.To = emailaddr
  36.         'oMail.Send
  37.         oMail.Display
  38.         Set oMail = Nothing
  39.         Set oApp = Nothing
  40.  
  41. End Sub
I just need to know how to run the following sql statement
Expand|Select|Wrap|Line Numbers
  1. SELECT ShippingAndDelivery.TrackingReceived, ShippingAndDelivery.TrackingNumber, ShippingAndDelivery.ShipMethod
  2. FROM Orders INNER JOIN ShippingAndDelivery ON Orders.PO_NUM = ShippingAndDelivery.PONbr
  3. WHERE (((Orders.PO_NUM)="MOMS00006806531"));
the PO_NUM will come from the form

Any help would be tremendous!
Jun 18 '14 #1
3 1577
zmbd
5,501 Expert Mod 4TB
Does your query work?
That is to say, if you set it up as a normal query does it return the information you desire?

If it does, then you have several ways of sending this as an attacment using the docmd.sendobject or you can build the message text by opening the query as a recordset within the VBA code and looping thru the records to build the message string, or thru even more advanced automation should you be using Outlook you could insert a table and then populate the table with the recordset results.
Jun 18 '14 #2
The query does work as I want it to and I an trying to build the message body as a record set right now. Im not after pretty - only functional at this moment. Plus most of the people I need to email only receive emails in text mode so a table would be not as effective. Can you point me to the correct recordset command to get what I need. I have made a little progress on the code:

Expand|Select|Wrap|Line Numbers
  1.         Dim rs As DAO.Recordset
  2.         Dim SQL As String
  3.  
  4.         SQL = "SELECT ShippingAndDelivery.TrackingReceived, " & _
  5.           "ShippingAndDelivery.TrackingNumber, ShippingAndDelivery.ShipMethod " & _
  6.           "FROM Orders INNER JOIN ShippingAndDelivery ON Orders.PO_NUM = ShippingAndDelivery.PONbr" & _
  7.           "WHERE (((Orders.PO_NUM)='" & PO & "'"
  8.  
  9.         Set rs = CurrentDb.OpenRecordset(SQL)
  10.  
  11.         'Check to see if the recordset actually contains rows
  12.         If Not (rs.EOF And rs.BOF) Then
  13.             rs.MoveFirst 'Unnecessary in this case, but still a good habit
  14.             Do Until rs.EOF = True
  15.  
  16.                 rs.someCommand to get the complete row
  17.                  there are 3 fields in the output
  18.  
  19.                 MyBodyText = "QUERY RESULTS SOMEHOW"
  20.  
  21.                 rs.MoveNext
  22.             Loop
  23.         Else
  24.             MsgBox "There are no records in the recordset."
  25.         End If
  26.  
  27.         MsgBox "Finished looping through records."
  28.  
  29.         rs.Close 'Close the recordset
  30.         Set rs = Nothing 'Clean up
Thank you for your reply and assistance!
Jun 18 '14 #3
zmbd
5,501 Expert Mod 4TB
It is very importaint that you properly format posted script, be it SQL, VBA, etc... or formated text (ie table) using the [CODE/] button in the format toolbar. It helps keep things like my enterprise AV from tripping malware warnings and it gives us a way to easily reference lines of code etc within your posts.

Rabbit has kindly done that for you in your last post (Thank You Rabbit (^_^) )

Starting on Line16:
Sorry to say there is no single command to pull the entire record at once (well there is; however, it pulls to multi-dimensional array so you end up looping.

actuall there are a few other things... I'll fix some here and then leave the rest for you to code...

Expand|Select|Wrap|Line Numbers
  1. 'This is air code... you need to finsh.
  2. Sub bytes_957268()
  3.     Dim zDB As DAO.Database
  4.     Dim zHoldString As String
  5.     Dim zMessage As String
  6.     Dim rs As DAO.Recordset
  7.     Dim SQL As String
  8.     '
  9.     '>In the SQL You have an undeclared variable
  10.     Dim PO As String
  11.     '
  12.     '>Error traps can be your friend
  13.     On Error GoTo zerr
  14.     '.
  15.     'SQL = "SELECT ShippingAndDelivery.TrackingReceived, " & _
  16.         "ShippingAndDelivery.TrackingNumber, ShippingAndDelivery.ShipMethod " & _
  17.         "FROM Orders INNER JOIN ShippingAndDelivery ON Orders.PO_NUM = ShippingAndDelivery.PONbr" & _
  18.         "WHERE (((Orders.PO_NUM)='" & PO & "'"
  19.     '
  20.     Set zDB = CurrentDb()
  21.     Set rs = zDB.OpenRecordset(Name:=SQL, Type:=adOpenDynamic)
  22.  
  23.     'Check to see if the recordset actually contains rows
  24.     'I don't depend on the EOF/BOF thing.... if there's even a single record then the count isn't zero.
  25.     'If Not (rs.EOF And rs.BOF) Then
  26.     If rs.RecordCount > 0 Then
  27.         rs.MoveFirst 'Unnecessary in this case, but still a good habit
  28.                         '>I wouldn't bet on this being unnecessary
  29.         With rs
  30.             Do
  31.                 'rs.someCommand to get the complete row
  32.                 ' - getrows method; however, it really doesn't help you here.
  33.                 'instead add the value of each field in the SQL:
  34.                 '
  35.                 zHoldString = !.TrackingReceived, & "," & '.... finish coding here
  36.                 zMessage = zMessage & zHoldString
  37.                 .MoveNext
  38.                 If Not .EOF Then zMessage = zMessage & Chr(10) & Chr(13)
  39.             Loop Until .EOF
  40.         End With
  41.     Else
  42.         MsgBox "There are no records in the recordset."
  43.     End If
  44.         MsgBox Prompt:=zMessage, Title:="Finished looping through records."
  45. ZCleanUp:
  46.     rs.Close
  47.     If Not rs Is Nothing Then Set rs = Nothing
  48.     If Not zDB Is Nothing Then Set zDB = Nothing
  49. Exit Sub
  50. zerr:
  51.     MsgBox Err.Number & vbCrLf & Err.description
  52.     Stop
  53.     Resume ZCleanUp
  54. End Sub
Jun 19 '14 #4

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

Similar topics

6
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the same form automatically. Does anybody know how...
0
by: Rob | last post by:
I doubt this is the best way to do it, but what I came up with was to hide the XML in an HTML Comment then edit the file deleting the HTML stuff and keep the XML results. If anyone has a better...
1
by: ljungers | last post by:
Hi and I hope that someone may have an answer for this, or an example of what I need to do. I have a Access database that a clerk will be entering a Order Number or Client Name or a Client City in a...
7
by: fcolon75 | last post by:
I'm an experienced Access user, but very new to coding VBA in Access. I'd like to do the following: 1) Develop a basic query in the query designer. 2) Call that query from a VBA script 3)...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
2
by: Himmel | last post by:
Hello! The reference database I currently use runs queries that pull data from hundreds of tables in order to create user-friendly form view. The problem is that these queries can take upwards of...
1
by: veaux | last post by:
Might not have explained this correctly in subject, but query results look like below: Name ID Phone Bill 001 123 Bill 001 234 Bill 001 ...
1
by: aaronkm | last post by:
Hello thescripts and well met. I've recently been handed a new duty and have the joy of 'crash coursing' MS Access. Things are working well but I've ran into a problem that I can't seem to find...
1
by: igor221189 | last post by:
Hello everyone. I have Access 2000 database which holds student records in the school.It stores subject grades for each student.In the 'Student Grade Form', I would like to search student surname...
1
by: Dave Mallett | last post by:
very new to Access. Trying to export query results via macro and transfertext, but keep getting error message stating "Microsoft Jet Engine cannot find the object 'HRQ-DM_Prd1_qtr.txt'. Make sure...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.