Access VBA OpenRecordset Group Level OutputTo No records error

Mac1206

Board Regular
Joined
Jun 3, 2016
Messages
184
Hi,

I have an issue when my code runs, it only grab the records that the cursor is on and not looping through to grab the specific records grouped by ID....For the 31 clients, ID 48961 which is 1 client of the 31 clients which the cursor was on in the Form was the only records pulled for the 31 different client groups...See Attachment Below...My issue is I'm only pulling records where my cursor is pointing to and not looping through all the records...Thanks

Private Sub cmdSendRpt_Click()
Dim rst As DAO.Recordset
Dim strSql As String

strSql = "Select * FROM Qry_Max_Pymt_PymtEmailDetail;"

Set rst = CurrentDb.OpenRecordset(strSql)

Do While Not rst.EOF

Me.txtID = rst![ID]

DoCmd.OutputTo acOutputQuery, "Max_Pymt_EmailDetails1", acFormatXLS, "C:\Users\amccoy\Desktop\TestFolder" & "\" & rst![ID] & "_" & rst![PymtsPayee] & ".xls"

DoEvents
rst.MoveNext

Loop

rst.Close
Set rst = Nothing

End Sub
 

Attachments

  • Queries.jpg
    Queries.jpg
    127.6 KB · Views: 14
  • GroupbyExport.jpg
    GroupbyExport.jpg
    45.8 KB · Views: 16
Thanks, could you please show me in sample code, I'm a visual person and learn through visual teachings. If I can visually see it then I can comprehend it for the most part and everything you are writing will make since...Sorry
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Ok, the simplest, though not the most efficient would be to just use the SendObject after creating the file.
DoCmd.SendObject acOutputQuery, "Max_Pymt_EmailDetails1", acFormatXLS, "C:\Users\amccoy\Desktop\TestFolder" & "\" & rst![ID] & "_" & rst![PymtsPayee] & ".xls"

Bring in ALL the email addresses in the first query and concatenate them with a semi colon ; and use that as the To address.

The email field is in the main query to include ID, Email, PymtsPayee, and the email addresses are separated by ; .... How do I get it to populate in the To: field in outlook, Add Subject, Add Message, and automatically send so the user doesn't have to press send 500 times...
 
Upvote 0
If you are a viaual person, then you should be checking out YouTube?, lots of video on there for all aspects of Access and a lot of other VBA.
This guy is very good, and there are plenty on examples using Google.

 
Upvote 0
This is my code but I'm getting the Compile error: Variable not defined so I'm missing something but don't know quite what?

Private Sub CmdEmailRpt_Click()

Dim rst As DAO.Recordset
Dim DB As DAO.Database
Dim strSql As String
Dim txtID As TempVars

Dim emailTo As String
Dim emailSubject As String
Dim emailText As String

Dim outApp As Outlook.Application
Dim outMail As Outlook.MailItem
Dim outStarted As Boolean

On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If outApp Is Nothing Then
Set outApp = CreateObject("Outlook.Application")
outStarted = True
End If

strSql = "Select * FROM Qry_Max_Pymt_PymtEmailDetail;"
Set DB = CurrentDb
Set rst = CurrentDb.OpenRecordset(strSql)

Do While Not rst.EOF

TempVars("txtID") = rst!ID.Value

emailTo = rs.Fields("ID").Value & " " & "<" & rs.Fields("email").Value & ";" & ">"
emailSubject = "This is a test"
emailText = "This is a test to ensure this is working properly"



Set outMail = outApp.CreateItem(olMailItem)
outMail.To = emailTo
outMail.Subject = emailSubject
outMail.Body = emailText
outMail.Send



DoCmd.SendObject acOutputQuery, "Max_Pymt_EmailDetails1", acFormatXLS, , , , , False

DoEvents
rst.MoveNext


Loop


rst.Close

Set rst = Nothing

If outStarted Then
outApp.Quit
End If

Set outMail = Nothing
Set outApp = Nothing

End Sub
 
Upvote 0
Well it will tell you what variable is not defined.?
Also please put code within code tags, and indented to make it easier to read.?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top