Saving Access03 Report to multiple pdf's

M

mccallen60

Hello,
I am a novice Access user. I have see several postings of questions like the
one I'm going to ask. I have a report I need to save into seperate pdf's. It
is a sales report broken into 52 territories. I want to run this report one
time (instead of 52 times) and have Access save it into 52 separate pdf's.
All the postings with similar questions refer to the following:
http://www.lebans.com/reporttopdf.htm
This function gets high praise. Unfortunately, I do not have any experience
working with this type of code. Can someone direct me to the area of the code
I would need to modify to look for a territory number, name the pdf with that
territory number and continue until it finishes? Thanks in advance for your
help.
 
D

dymondjack

The nice thing about using code like this is that you very rarely ever have
to modify it. Especially for a situation like yours.

The way you're going to want to do this is not to modify any of Lebans'
existing code, but rather write your own snippet and reference his function.
That way, his code is still untouched for everything else you want to use it
for.

Here's a breakdown (I'm a bit busy and don't have time to write out a full
code example, but this should get you started).


Sub PrintTerritories()

'Gather a list of the territories (via SQL Select statement presumably)

'Loop through each item in the list, running the ReportToPDF function
While not YourList.EOF

'Set the path for the report in a variable
var = "MyReportPath"

'Print the report to pdf
ReportToPDF "reportname", PathVariable


Wend

End Sub


Something like that anyway... don't bother trying to use the code I've
written, but the structure is there. Then you call on this procedure
(PrintTerritories) which in turn prints out 52 of the same report to
different paths. No changes necessary in Leban's code.

Also, you can add in report criteria in a similar fashion, say to print out
territory reports only specific to whatever state your working with.

hth

--
Jack Leach
www.tristatemachine.com

- "Success is the ability to go from one failure to another with no loss of
enthusiasm." - Sir Winston Churchill
 
M

mccallen60

Hello Jack,
Thanks for your prompt reply. Unfortunately, I'm VB ignorant (to the
extreme). Currently, I'll keep running the process 52 times to create my
individual pdf's until I can take a class on VB. It sounds like a helpful
tool.
Thanks again. Kevin
 
D

dymondjack

If you want to give an explanation of exactly what you are doing now, I'm
sure someone could help automate it for you.

Is your report a little bit different for each of the 52 times you run it
(the same report name but with different information for each state?), or are
you just trying to make 52 copies. If you just want to make copies its
basically a three line procedure, doesn't really get any easier.

Either way this is usually a fairly simple task for vba. If you want to
give it a go post your structure and I'll take a look. Nothing worse than
doing things twice if you can tell something to do it once and get the same
result. In fact, that's exactly how I wound up here <g>

Either way

--
Jack Leach
www.tristatemachine.com

- "Success is the ability to go from one failure to another with no loss of
enthusiasm." - Sir Winston Churchill
 
M

mccallen60

The report is fairly simple. It shows cutomer activity per territory. We have
52 territories. Each territory has it's own ID number. I need to create a pdf
for each territory number. The territory ID is set up in the table I'm using
for the report. I just need to know how to write a VB code to create a
separate report for each of our territories so our sales reps can see how
their customers are performing.
Hope this helps.
 
D

dymondjack

Kevin,

I'm pretty sure I've got a way to do this, but I would like to test it first
and make sure everything's up to snuff. I should be able to do this over the
weekend at some point, so check back in in a few days. The code itself
should be fairly simple if what I'm thinking works, but I don't want to waste
your time having you work with an untested idea/code.

--
Jack Leach
www.tristatemachine.com

- "Success is the ability to go from one failure to another with no loss of
enthusiasm." - Sir Winston Churchill
 
D

dymondjack

Ok, got it. Took a day longer than I expected for me to get to it but here
it is.

The only way I could think to do this was with a public variable, aside from
editing Leban's code itself, which I wanted to avoid. Usually I try and
avoid public vars, but I didn't think there was a way to pass a where
condition to the report through Leban's function (this may be doable, I
didn't even try it thought).

Essentially, what we're doing is creating a loop for the ReportToPDF
funtion. This loop will also increment our public variable by 1 each time
the code loops. The public var serves two purposes: it provides us with a
way to make separate filenames for each report, and more importantly, it sets
the recordsource for the report in it's Open event.

So, assuming that your report ID's are 1 through 52, this is what I came up
with.

In a standard module (preferably a new, though it doesn't really matter),
you need to create a public variable to hold the value of the current report
ID. (public vars are created after the Option codes at the beginning of the
module, but before any subs or functions).

So you'll see this:

'====CODESTART

Option Compare Database
Option Explicit

Public gTerritoryID As Long

'====CODE END



Next, go to your report, and in the report's OnOpen event, you need to limit
the records to those with the specified territory ID (which is held in our
new global variable, and will be set later).

So you should see this in your report:

'====CODESTART

Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = "SELECT * FROM tablname " _
"WHERE [IDfield] = " & gTerritoryID
End Sub

'====CODEEND



You may have to modify the SQL line a bit, I'm not sure what your details
are, but this will limit the records to whatever ID matches the value of
gTerritoryID. You may want to test this for accuracy before going any
further. The thing to remember here is that we need to make sure
gTerritoryID actually has a value before we try and run the report, as there
is no handling for it in the report itself.

The last part of this is to write a function that loops from 1 to 52,
handling the report and filenames at the same time. This could go in the
same module we put the Public Var in. See below:


'====CODESTART

Option Compare Database
Option Explicit

Public gTerritoryID As Long


Public Function PrintTerritoryReports() As Boolean
Dim lngCount As Long 'our counter from 1 to 52
Dim strFile As String 'our output file to pass to the function

'Initialize the counter
lngCount = 1

'Loop while counter is less than 53
While lngCount < 53

'Set the global variable for when the report is printed
gTerritoryID = lngCount

'Set the Filepath
'(make sure the folder exists, it will not be created for you)
strFile = "C:\YourFolder\Territory" & Trim(Str(gTerritoryID)) & ".pdf"

'Print the report
ConvertReportToPDF "Reportname", , strFile, False, False

'Increment the counter (will update next record and filename)
lngCount = lngCount + 1

Wend

'Reset the global var (not required, but cleanup is always nice)
gTerritoryID = 0

End Function

'====CODE END



That should do it. I just got done testing this before I posted, so you
should be all set. I wasn't sure that the Open event would run and
effectively restrict records when printed, but as it turns out it does. This
was tested on a Vista system using Access 2003, though I don't see it making
a difference either way.

If you have any more questions about how it works or need a hand getting it
set up with details specific to your code, let me know.

hth


--
Jack Leach
www.tristatemachine.com

- "Success is the ability to go from one failure to another with no loss of
enthusiasm." - Sir Winston Churchill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top