473,386 Members | 1,793 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.

Setting variables from current record for use in email subject and body

2
Hello, I have a parts db that I am trying to send a request to re-order email when the parts quantity is low. From the form the user opens a sub form to edit the parts details and if it needs to be ordered the can hit the button and it will send out the email.

I am trying to put the item name from the Item field and put it in the email subject and then pull the Item, Part Number and Quantity to use in the emails body. I have been scouring the web for possible answers, the email gets sent but only with the text for the code. I have tried using Dlookup and SQL expressions but I don't have a where criteria, I just want to pull from the current record that is opened in the sub form.
Parts is the Table.
Item, PartNumber and Quantity are the fields/columns

Expand|Select|Wrap|Line Numbers
  1. Private Sub ReOrderEmail_Click()
  2.  
  3.     Dim olApp As Object
  4.     Dim objMail As Object
  5.     Dim Mailsql As String
  6.     Dim Subsql As String
  7.  
  8.     ' Puts date in reordered date field
  9.     Me.txtReOrderedDate = Date
  10.  
  11.     Dim rs As DAO.Recordset
  12.  
  13.     Subsql = CurrentDb.OpenRecordset("SELECT Item FROM Parts")
  14.     Mailsql = CurrentDb.OpenRecordset("SELECT Item,Quantity FROM Parts")
  15.  
  16.     'Sends Email
  17.     On Error Resume Next
  18.     Set olApp = GetObject(, "Outlook.Application")
  19.  
  20.      If Err Then
  21.         Set olApp = CreateObject("Outlook.Application")
  22.      End If
  23.  
  24.     Set objMail = olApp.CreateItem(olMailItem)
  25.  
  26.     With objMail
  27.       .To = "address@company.com"
  28.       .Subject = "Please Reorder" & Subsql
  29.       .Body = Mailsql
  30.       .send
  31.     End With
  32.  
  33.    MsgBox "The email has been sent."
  34.  
  35. rs.Close
  36.  
  37. Set rs = Nothing
  38.  
  39.  
  40. End Sub
  41.  
This will be sent to same person everytime, and just need a simple email for them to see to re-order more parts.
It has changed numerous times, but this is where I currently am at, thanks in advance for your help.
Jun 20 '15 #1

✓ answered by zmbd

If you use Bytes.com search tool with EMAIL and one of the moderator/experts names (NeoPa, Rabbit, ZMBD, etc...) you will turn up a treasure-trove of threads covering this very topic in its coat of many colours.

You'll also find an invaluable source of information in our insight's section:
Bytes > Sitemap > Microsoft Access / VBA Insights


Let us get to your specific post:
Ok, there are a few minor problems with your code; however, I need a bit of information about your form and record set.

1) From your post it appears that the form is bound to a table/query (?), if so, then you can simply use the current values in the control for your email. Based on the post you've already given us that Item, PartNumber and Quantity are the fields/columns in the form's recordset and if these are bound to controls on the form and current record then the code only need to reference them for their current values, example:

Expand|Select|Wrap|Line Numbers
  1.  zOrderItem = Me.Item
  2. zPartNumber = Me.PartNumber
  3. zQnty = Me.Quantity
  4.  
You can then build your message string text using the variables. (It's not strickly required to use the variable approach; however, find the code easier to write)

2) Multiple use of CurrentDb isn't considered pest practice. This potentially opens multiple pointers to the current database and allocates memory for each. Instead it is a preferred method to use something like:

Expand|Select|Wrap|Line Numbers
  1. Dim zSQL As String
  2.      Dim zDB As DAO.Database
  3.      Dim zRS As DAO.Recordset
  4.  
  5.      ' ...other code
  6.  
  7.      'Setup the recordset
  8.      Set zDB = CurrentDb
  9.      zSQL = "SELECT Item FROM Parts"
  10.      Set zRS = zDB.OpenRecordset(zSQL, dbOpenDynaset)
  11.  
You should also close and release objects etc... that you've opened:

Expand|Select|Wrap|Line Numbers
  1. If Not isnothing(zRS) Then
  2.    zRS.Close
  3.    Set zRS = Nothing
  4. End If
  5.    'note we didn't open the current database
  6.    '    in code so we're only releasing the pointer
  7. If Not isnothing(zDB) Then Set zDB = Nothing
  8. '
  9. 'and close and release any other objects too
3) Given that you are using outlook as the email client, what appears to be a fairly short text message, then a widely available code snipette is from:
DoCmd.SendObject Method
Frankly, I use a variation of this function to send error reports from my projects
Expand|Select|Wrap|Line Numbers
  1. Public Function CreateEmailWithOutlook( _
  2.     MessageTo As String, _
  3.     Subject As String, _
  4.     MessageBody As String)
  5.  
  6.     ' Define app variable and get Outlook using the "New" keyword
  7.     Dim olApp As New Outlook.Application
  8.     Dim olMailItem As Outlook.MailItem  ' An Outlook Mail item
  9.  
  10.     ' Create a new email object
  11.     Set olMailItem = olApp.CreateItem(olMailItem)
  12.  
  13.     ' Add the To/Subject/Body to the message and display the message
  14.     With olMailItem
  15.         .To = MessageTo
  16.         .Subject = Subject
  17.         .Body = MessageBody
  18.         .Display    ' To show the email message to the user
  19.     End With
  20.  
  21.     ' Release all object variables
  22.     Set olMailItem = Nothing
  23.     Set olApp = Nothing
  24.  
  25. End Function
  26.  
Build your string and pass to this function the other parameters voila the email appears. NOW THERE IS A CATCH TO THIS ONE - If the user cancels the email, there can be an error so I use a variation that does not display the email message to the user and a version can be found at the above link. I use a message box with yes/no buttons to confirm the desire to send the email. The MsgBox displays the recipient(s) email and the text...

BOL

-z

2 2970
zmbd
5,501 Expert Mod 4TB
If you use Bytes.com search tool with EMAIL and one of the moderator/experts names (NeoPa, Rabbit, ZMBD, etc...) you will turn up a treasure-trove of threads covering this very topic in its coat of many colours.

You'll also find an invaluable source of information in our insight's section:
Bytes > Sitemap > Microsoft Access / VBA Insights


Let us get to your specific post:
Ok, there are a few minor problems with your code; however, I need a bit of information about your form and record set.

1) From your post it appears that the form is bound to a table/query (?), if so, then you can simply use the current values in the control for your email. Based on the post you've already given us that Item, PartNumber and Quantity are the fields/columns in the form's recordset and if these are bound to controls on the form and current record then the code only need to reference them for their current values, example:

Expand|Select|Wrap|Line Numbers
  1.  zOrderItem = Me.Item
  2. zPartNumber = Me.PartNumber
  3. zQnty = Me.Quantity
  4.  
You can then build your message string text using the variables. (It's not strickly required to use the variable approach; however, find the code easier to write)

2) Multiple use of CurrentDb isn't considered pest practice. This potentially opens multiple pointers to the current database and allocates memory for each. Instead it is a preferred method to use something like:

Expand|Select|Wrap|Line Numbers
  1. Dim zSQL As String
  2.      Dim zDB As DAO.Database
  3.      Dim zRS As DAO.Recordset
  4.  
  5.      ' ...other code
  6.  
  7.      'Setup the recordset
  8.      Set zDB = CurrentDb
  9.      zSQL = "SELECT Item FROM Parts"
  10.      Set zRS = zDB.OpenRecordset(zSQL, dbOpenDynaset)
  11.  
You should also close and release objects etc... that you've opened:

Expand|Select|Wrap|Line Numbers
  1. If Not isnothing(zRS) Then
  2.    zRS.Close
  3.    Set zRS = Nothing
  4. End If
  5.    'note we didn't open the current database
  6.    '    in code so we're only releasing the pointer
  7. If Not isnothing(zDB) Then Set zDB = Nothing
  8. '
  9. 'and close and release any other objects too
3) Given that you are using outlook as the email client, what appears to be a fairly short text message, then a widely available code snipette is from:
DoCmd.SendObject Method
Frankly, I use a variation of this function to send error reports from my projects
Expand|Select|Wrap|Line Numbers
  1. Public Function CreateEmailWithOutlook( _
  2.     MessageTo As String, _
  3.     Subject As String, _
  4.     MessageBody As String)
  5.  
  6.     ' Define app variable and get Outlook using the "New" keyword
  7.     Dim olApp As New Outlook.Application
  8.     Dim olMailItem As Outlook.MailItem  ' An Outlook Mail item
  9.  
  10.     ' Create a new email object
  11.     Set olMailItem = olApp.CreateItem(olMailItem)
  12.  
  13.     ' Add the To/Subject/Body to the message and display the message
  14.     With olMailItem
  15.         .To = MessageTo
  16.         .Subject = Subject
  17.         .Body = MessageBody
  18.         .Display    ' To show the email message to the user
  19.     End With
  20.  
  21.     ' Release all object variables
  22.     Set olMailItem = Nothing
  23.     Set olApp = Nothing
  24.  
  25. End Function
  26.  
Build your string and pass to this function the other parameters voila the email appears. NOW THERE IS A CATCH TO THIS ONE - If the user cancels the email, there can be an error so I use a variation that does not display the email message to the user and a version can be found at the above link. I use a message box with yes/no buttons to confirm the desire to send the email. The MsgBox displays the recipient(s) email and the text...

BOL

-z
Jun 21 '15 #2
Steve3
2
That did the trick, for whatever reason I wasn't having very good luck assigning values using me."field" for the current recordset, able to get it working now. Thank you for you assistance, I will check out the Insights section as well.
Jun 24 '15 #3

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

Similar topics

1
by: WindAndWaves | last post by:
Hey Folks I seem to be asking millions of questions. Truth is, I just discovered this awesome research and having been typing by myself for years the questions are all coming out. Here is...
1
by: Richard Coutts | last post by:
I have a Continuous Form where each record has a button that activates another form that simplifies entering values into the record. The activated form has the equivalent of a "Done" button. I'd...
3
by: Maria | last post by:
Is there another way to delete the current record in a subform from the main form, another subform or a sub-subform other than setting focus on a field in the subform and using run command...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
2
by: Fabio Negri Cicotti [MCP] | last post by:
Hi all. How do I do to display a picture on e-mail's body when visualized using Outlook Express? I've set the properties bellow but till now I've got the image shown as that box with a red...
1
by: Michael Fitzpatrick | last post by:
Is it possible to set a variable with the value of a compile time constant. Example: #Const MODE_STR = "Mode1" dim strMode as String = MODE_STR Obviously this doesn't work. Is there a way...
2
by: Nita | last post by:
Hi, I have no background in scripting (barr HTML) and need some help with hashing out a VB script for a database I'm working on. What my goal is, is to have a single record of a form which is...
3
by: Kosmos | last post by:
Hey ya'll...I can't seem to figure out why I'm getting this error message, but it all started when I added the new line of code with the recSet5.AddNew --- when I ran the first line, the logic worked...
1
vikas251074
by: vikas251074 | last post by:
Hello sir, I am facing some problem for which I am trying hard to solve it. But in vain. This programe is for View/Deletion. When I run program for first time, I select vlan from list and press...
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: 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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.