Email records on Form to body of email (1 Viewer)

Lennie

Registered User.
Local time
Today, 23:01
Joined
Apr 20, 2017
Messages
11
Hi, was wondering if someone can please take a look at my code because it is not giving me the output on the email body.
Basically i have a button on my form that on the event it is clicked it should export all the records on the current form to the body of my email. The problem with my current code is it exports all the records on the query and not the form. When i change db.OpenRecordset ("qry_IRS Query",dbOpenDynaset) to ("frm_IRS Query",dbOpenDynaset) nothing appears on the email body and i get a run time error 3078 - cannot find input table or query frm_IRS Query. Can someone kindly help please. Thanking whoever in advance for your assistance. My current code as below:

Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim olItem As Variant
Dim olMailItem As Variant
Dim olApp As Object
Dim strTo As String
Dim strcc As String
Dim strBody As String

Set olApp = CreateObject("outlook.application")
Set olItem = olApp.CreateItem(olMailItem)

olItem.Display
olItem.To = "email address"
olItem.cc = "email address"
olItem.Subject = "Derivatives Confirmation"
olItem.Body = "Please confirm" & vbCrLf & "or i will kick you" & vbCrLf & vbCrLf

Set db = CurrentDb
Set rec = db.OpenRecordset("qry_IRS Query", dbOpenDynaset)

With rec
If Not (rec.BOF And rec.EOF) Then
rec.MoveLast
rec.MoveFirst
intCount = rec.RecordCount
For intLoop = 1 To intCount
olItem.Body = olItem.Body & rec("ContractNo") & rec("ClientName") & rec("RateSet Date") & rec("Pay/Rec") & rec("Currency") & rec("Amount")
rec.MoveNext
Next intLoop

End If
End With

Set olApp = Nothing
Set olItem = Nothing
 

Cronk

Registered User.
Local time
Today, 23:01
Joined
Jul 4, 2013
Messages
2,774
Set rec = db.OpenRecordset(me.recordsource)
 

Lennie

Registered User.
Local time
Today, 23:01
Joined
Apr 20, 2017
Messages
11
thanks for looking into it Cronk. I changed it to what you suggested but still gave me the same results ie still displaying the entire query as opposed to just the records on the form
 

Cronk

Registered User.
Local time
Today, 23:01
Joined
Jul 4, 2013
Messages
2,774
My code opens a recordset containing all the records in the form holding the button clicked. Is the form filtered?
 

Lennie

Registered User.
Local time
Today, 23:01
Joined
Apr 20, 2017
Messages
11
There is a date filter that a user can enter and search by. The records on the form will be data between those dates.
 

Cronk

Registered User.
Local time
Today, 23:01
Joined
Jul 4, 2013
Messages
2,774
The filter is relevant

Use
set rec= me.recordsetclone
 

Lennie

Registered User.
Local time
Today, 23:01
Joined
Apr 20, 2017
Messages
11
oh wow works perfectly. Thanking you so much Cronk. thank you thank you
 

Users who are viewing this thread

Top Bottom