VBA Loop One Report - output and save many pdf's

wstandis

Registered User.
Local time
Today, 05:18
Joined
May 4, 2009
Messages
16
Hello I am hoping that someone here can help me I have an have a database with a very simple table with about 12500 records format is as follows

Example:
(here it s pipe delimited just for illustration purposes)
Dicks Sporting Goods | Widget 1 | $7,000 | Jan
Dicks Sporting Goods | Widget 2 | $6,000 | Feb
Dicks Sporting Goods | Widget 3 | $5,000 | Mar
Dicks Sporting Goods | Widget 4 | $4,000 | April
Dicks Sporting Goods | Widget 5 | $3,000 | May
Dicks Sporting Goods | Widget 6 | $2,000 | June
Dicks Sporting Goods | Widget 7 | $1,000 | July
Sports Authority | Widget 1 | $7,000 | Jan
Sports Authority | Widget 2 | $6,000 | Feb
Sports Authority | Widget 3 | $5,000 | Mar
Sports Authority | Widget 4 | $4,000 | April
Sports Authority | Widget 5 | $3,000 | May
Sports Authority | Widget 6 | $2,000 | June
Sports Authority | Widget 7 | $1,000 | July

I will build a simple query on this table that presents me the data i need - then I will build a very simple report - I also have pdf writer so i already know how to print and save these reports to a pdf.

What I desperately need is a way to automatically loop through the Query and for each new store name save a report to a directory i define with the store name as the report name.

Example: Report 1 would be - Dicks Sporting Goods.pdf and contain the following data:
Dicks Sporting Goods | Widget 1 | $7,000 | Jan
Dicks Sporting Goods | Widget 2 | $6,000 | Feb
Dicks Sporting Goods | Widget 3 | $5,000 | Mar
Dicks Sporting Goods | Widget 4 | $4,000 | April
Dicks Sporting Goods | Widget 5 | $3,000 | May
Dicks Sporting Goods | Widget 6 | $2,000 | June
Dicks Sporting Goods | Widget 7 | $1,000 | July


Example: Report 2: Sports Authority.pdf and contain the following data.
Sports Authority | Widget 1 | $7,000 | Jan
Sports Authority | Widget 2 | $6,000 | Feb
Sports Authority | Widget 3 | $5,000 | Mar
Sports Authority | Widget 4 | $4,000 | April
Sports Authority | Widget 5 | $3,000 | May
Sports Authority | Widget 6 | $2,000 | June
Sports Authority | Widget 7 | $1,000 | July

I have 160 unique stores and do not want to have to set up a report for each as when they change that would be a data nightmare,
 
I am afraid this may be to advaced for me are there any better exanmples? This sends me to about 5 different places to pull pieces of each.
 
Your previous post didn't show any attempt at trying to do it yourself, though most of the pieces are there. Dive in and I'll be glad to help you if you get stuck. I don't have time to write it all for you; I'm too busy earning my salary to earn yours for you.
 
Paul-

i have just had a quick look at this -would this not email to all in the table
rather to 1 person then another ??

i have had to do emailings out and have had to do on mass broadcasting emails (400-500 email address in the bcc field and my email adress in th to field )

i am not one for sending annoying emails out - but it was a request of a client to all of their sub contractors and i had to do it - i would of rather sent individual emails out ...rather than to all in the bcc field (this was the only way i could send to all without the subcontractos seeing the other persons address -- data protection stuff
 
No, if you follow one down:

http://www.granite.ab.ca/access/email/recordsetloop.htm

You use the loop in combination with the other code to send an individual report to each person. The key would be if you send within the loop, you send to that person. You could build a string in the loop then send after the loop, which would send one email out. The OP's need is slightly different, but they have already stated they know how to save the PDF files.
 
No, if you follow one down:

http://www.granite.ab.ca/access/email/recordsetloop.htm

You use the loop in combination with the other code to send an individual report to each person. The key would be if you send within the loop, you send to that person. You could build a string in the loop then send after the loop, which would send one email out. The OP's need is slightly different, but they have already stated they know how to save the PDF files.

thats one for later for me to diegest
but thanks for the response -- (something thats on the back burner - so I try to remember it)
ta
 
Ok So here I my questions: I am good with making this an on open event

Me.Filter = "ShipmentsID=" & Forms![Select Load List]![LoadID]
Me.FilterOn = True
Would “ShipmentsID” be the unique ID that identifies my distribution channel or recipetient? In my case “StoreID” which is unique to each store? Ie. Dicks Sporting goods

I would also assume that Forms![Select Load List]![LoadID] referes to the form where I create teh button to run the following code: What would Load ID be assocuiated with?


On Local Error GoTo Some_Err

Dim MyDB As Database, RS As Recordset
Dim strBody As String, lngCount As Long, lngRSCount As Long

DoCmd.RunCommand acCmdSaveRecord
Set MyDB = DBEngine.Workspaces(0).Databases(0)

Me!txtProgress = Null
The above all seems like generic text setting the stage for what follows?

Set RS = MyDB.OpenRecordset _ ("Email - Outstanding Promos")
Would this be the name of the report that I am opening? In my case “Store invoices” or the query the report is based on? “Commissions”

lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
MsgBox "No promo email messages to send.", vbInformation

Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF
lngCount = lngCount + 1
lblStatus.Caption = "Writing Message " & CStr(lngCount) _ & " of " & CStr(lngRSCount) & "..."

The above seems like a status which I can add after i figure out the other functionality? The section below seems specific to the email functionality Do I need this part? It looks like it is pulling information from a query as well as updating information?

strTo = RS!cEmailAddress Is this the report name or underlying query name?

intMessageID = Year(Now) & Month(Now) & Day(Now) & Fix(Timer) & "_MabryMail" is this a field in the query?

' Send the email using some technique or other this is where I would input my code to create a pdf???

RS.Edit RS("cpeDateTimeEmailed") = Now() looks like it is updating a field in the query?

RS.Update RS.MoveNext Loop End If RS.Close MyDB.Close Set RS = Nothing Set MyDB = Nothing Close

The section below seems like it is just to make a fancy interface to let you know status? Not sure if this is needed?

Me!txtProgress = "Sent " & CStr(lngRSCount) & " emails."
lblStatus.Caption = "Email disconnected"
MsgBox "Done sending Promo email. ", vbInformation, "Done" lblStatus.Caption = "Idle..."
Exit Sub

Some_Err:
'MousePointer = 0
MsgBox "Error (" & CStr(Err.Number) & ") " & Err.Description, _
vbExclamation, "Error!"
lblStatus.Caption = "Email disconnected"
 
Last edited:
In answer to the first question: yes, ShipmentID would be the primary key, and it sounds like you'd replace that with StoreID.

For the rest, somehow the code got all jammed together, making it difficult to follow. Can you repost that so it keeps the formatting? You probably want the code tags (
Code:
your code here[/ code], without the space).
 
Never mind, you were a step ahead of me.
On Local Error GoTo Some_Err

Dim MyDB As Database, RS As Recordset
Dim strBody As String, lngCount As Long, lngRSCount As Long

DoCmd.RunCommand acCmdSaveRecord
Set MyDB = DBEngine.Workspaces(0).Databases(0)

Me!txtProgress = Null
The above all seems like generic text setting the stage for what follows? Pretty much, yes, and you probably don't care about the last line

Set RS = MyDB.OpenRecordset _ ("Email - Outstanding Promos")
Would this be the name of the report that I am opening? In my case “Store invoices” or the query the report is based on? “Commissions” Either the query the report is based on, or since that query probably returns multiple lines for each store, an SQL statement like "SELECT DISTINCT StoreID FROM QueryName" so you only get one instance of each store

lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
MsgBox "No promo email messages to send.", vbInformation

Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF
lngCount = lngCount + 1
lblStatus.Caption = "Writing Message " & CStr(lngCount) _ & " of " & CStr(lngRSCount) & "..."

The above seems like a status which I can add after i figure out the other functionality? The section below seems specific to the email functionality Do I need this part? It looks like it is pulling information from a query as well as updating information? My only criticism of this code is that it contains some extra stuff that makes it more confusing for a beginner. Yes, some of it is either updating the screen to keep the user updated or updating the table to record the fact that the email was sent, both of which can be dropped. You don't need those last 2 lines

strTo = RS!cEmailAddress Is this the report name or underlying query name? That is a field in the query to get the email address - you don't need

intMessageID = Year(Now) & Month(Now) & Day(Now) & Fix(Timer) & "_MabryMail" is this a field in the query? They're building a string that they never use - you don't need

' Send the email using some technique or other this is where I would input my code to create a pdf??? Yes, exactly. Right before this line, you would want to populate the form control above to the current store, like:

Forms![Select Load List]![LoadID] = rs!StoreID


RS.Edit RS("cpeDateTimeEmailed") = Now() looks like it is updating a field in the query? Yes, you don't need

RS.Update RS.MoveNext Loop End If RS.Close MyDB.Close Set RS = Nothing Set MyDB = Nothing Close

The section below seems like it is just to make a fancy interface to let you know status? Not sure if this is needed? Yes, exactly, and you don't need the first line or either of the lblStatus lines

Me!txtProgress = "Sent " & CStr(lngRSCount) & " emails."
lblStatus.Caption = "Email disconnected"
MsgBox "Done sending Promo email. ", vbInformation, "Done" lblStatus.Caption = "Idle..."
Exit Sub

Some_Err:
'MousePointer = 0
MsgBox "Error (" & CStr(Err.Number) & ") " & Err.Description, _
vbExclamation, "Error!"
lblStatus.Caption = "Email disconnected"
 
In answer to the first question: yes, ShipmentID would be the primary key, and it sounds like you'd replace that with StoreID.

For the rest, somehow the code got all jammed together, making it difficult to follow. Can you repost that so it keeps the formatting? You probably want the code tags (
Code:
your code here[/ code], without the space).[/quote]
 

Recap I have one table with 6 fields[LIST=1]
[*]ID
[*]Store_ID
[*]Store Name
[*]Item
[*]Amt
[*]Month
[/LIST]A query built on this table called test - with all fields
A report built on thsi query Called [FONT=Tms Rmn]Commissions_Rpt[/FONT]
A form built on this query called [FONT=Tms Rmn]Commissions_Frm[/FONT]
 
 
Thanks - so just to clarify on my report on event command i would use:
 
[FONT=Courier New]Me.Filter = "ShipmentsID=" & Forms![[FONT=Tms Rmn]Commissions_Frm[/FONT]]![Store_ID]
Me.FilterOn = True[/FONT]

[FONT=Courier New][COLOR=red][B]what is the Shipments ID?[/B][/COLOR][/FONT]


[FONT=Courier New]To consolidate the code based on your last suggestion: I now have:[/FONT]

On Local Error GoTo Some_Err
 
Dim MyDB As Database, RS As Recordset
Dim strBody As String, lngCount As Long, lngRSCount As Long
DoCmd.RunCommand acCmdSaveRecord
Set MyDB = DBEngine.Workspaces(0).Databases(0)
 
 
Set RS = MyDB.OpenRecordset _
("SELECT DISTINCT Store_ID FROM test")
 
 
lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
MsgBox "No Comission Schedules to Print", vbInformation
Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF
 
Forms![[FONT=Tms Rmn]Commissions_Frm[/FONT]]![Store_ID] = RS!Store_ID
Beep 
' Want to get the rest right before I start messing with the output.
 
 
RS.Edit
RS.Update
RS.MoveNext
Loop
 
End If
RS.Close
MyDB.Close
Set RS = Nothing
Set MyDB = Nothing
Close
 
 
MsgBox "Done sending Promo email. ", vbInformation, "Done"
 
Exit Sub
 
Some_Err:
MsgBox "Error (" & CStr(Err.Number) & ") " & Err.Description, _
vbExclamation, "Error!"
lblStatus.Caption = "Email disconnected"
End Sub
 
Last edited:
First make sure in Tools/References that the Microsoft DAO 3.6 library is checked. Also, the form should probably not be bound to the table or query, or at least make sure the textbox you use for the report filter is not bound.
Thanks - so just to clarify on my report on event command i would use:

Me.Filter = "ShipmentsID=" & Forms![Commissions_Frm]![Store_ID]
Me.FilterOn = True


what is the Shipments ID? As noted previously, you would replace that with Store_ID


To consolidate the code based on your last suggestion: I now have:

On Local Error GoTo Some_Err

Dim MyDB As DAO.Database, RS As DAO.Recordset
Dim strBody As String, lngCount As Long, lngRSCount As Long
DoCmd.RunCommand acCmdSaveRecord
Set MyDB = DBEngine.Workspaces(0).Databases(0)


Set RS = MyDB.OpenRecordset _
("SELECT DISTINCT Store_ID FROM test")


lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
MsgBox "No Comission Schedules to Print", vbInformation
Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF

Forms![Commissions_Frm]![Store_ID] = RS!Store_ID
Beep
' Want to get the rest right before I start messing with the output.
Debug.Print RS!Store_ID 'this will put each store ID in the VBA immediate window, so you can see that it spun through them. Only needed for testing

RS.Edit Delete
RS.Update Delete
RS.MoveNext
Loop

End If
RS.Close
MyDB.Close
Set RS = Nothing
Set MyDB = Nothing
Close


MsgBox "Done sending Promo email. ", vbInformation, "Done"

Exit Sub

Some_Err:
MsgBox "Error (" & CStr(Err.Number) & ") " & Err.Description, _
vbExclamation, "Error!"
lblStatus.Caption = "Email disconnected" Delete
End Sub
 
First make sure in Tools/References that the Microsoft DAO 3.6 library is checked. Also, the form should probably not be bound to the table or query, or at least make sure the textbox you use for the report filter is not bound.

Ok I checked the Tools/References and DAO 3.6 library is checked

I removed the association with the query - the form is now unbound. Question should I have the field on the form something other than Store_ID?

Also do I have to enter a value in here before I pres the button?

Quote:
Originally Posted by wstandis
Thanks - so just to clarify on my report on event command i would use:

Me.Filter = "ShipmentsID=" & Forms![Commissions_Frm]![Store_ID]
Me.FilterOn = True


what is the Shipments ID? As noted previously, you would replace that with Store_ID

Ok I have updated it to say
Me.Filter = "Store_ID=" & Forms![Commissions_Frm]![Store_ID]
Me.FilterOn = True

question though? When does the report get opened to utilize this code?


To consolidate the code based on your last suggestion: I now have:

On Local Error GoTo Some_Err

Dim MyDB As DAO.Database, RS As DAO.Recordset Done
Dim strBody As String, lngCount As Long, lngRSCount As Long
DoCmd.RunCommand acCmdSaveRecord
Set MyDB = DBEngine.Workspaces(0).Databases(0)

Above has been Done
Set RS = MyDB.OpenRecordset _
("SELECT DISTINCT Store_ID FROM test")


lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
MsgBox "No Comission Schedules to Print", vbInformation
Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF

Forms![Commissions_Frm]![Store_ID] = RS!Store_ID
Beep
' Want to get the rest right before I start messing with the output.
Debug.Print RS!Store_ID 'this will put each store ID in the VBA immediate window, so you can see that it spun through them. Only needed for testing
Above has been done


RS.Edit Delete Done
RS.Update Delete Done
RS.MoveNext
Loop

End If
RS.Close
MyDB.Close
Set RS = Nothing
Set MyDB = Nothing
Close


MsgBox "Done sending Promo email. ", vbInformation, "Done"

Exit Sub

Some_Err:
MsgBox "Error (" & CStr(Err.Number) & ") " & Err.Description, _
vbExclamation, "Error!"
lblStatus.Caption = "Email disconnected" Delete Done
End Sub

__________________

After all of this i get "Error (3265) Item not found in this collection"
 
When you print the report to pdf, it will use that code to filter the report to the current store. You don't need to enter a value before starting the process. The code will put values in as needed. Comment out this line (put a single quote at the beginning):

On Local Error GoTo Some_Err

Then try to run the code. That should bring you to the exact line causing the error. Let me know which it is. It sounds like either a field name or the control on the form is misspelled.
 
Paul You Are By far the most helpful online friend I have ever met. I was able to play around with this last night and I have it working.

This has been a great learning experience and I thank you very much for your assistance.

One virtual beer heading your way~!!!!!!!
 
Outstanding! I will virtually enjoy the virtual beer. :p
 

Users who are viewing this thread

Back
Top Bottom