473,396 Members | 1,599 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,396 software developers and data experts.

Export Access Rpt to single .pdfs on network using variable folder names

11
Hi...I could use assistance from someone with more expertise than I. Dealing with this type of code in Access is new for me

I am trying to export a report containing multiple letters to people (from Access 2012) to individual .pdf files on the network. I understand I need a loop to do this which I have never done before. The path to save the letters in (folder and subfolder) as well as the file name should be derived from values/columns contained in the table or query which the report is written on. I am using ADO commands but not even sure that is the best option as I have found conflicting information as to which is best ADO or DAO. Any assistance will be so greatly appreciated. kindest regards.
Oct 31 '12 #1

✓ answered by TheSmileyCoder

I dont know if the Docmd.OutputTo will create the directory for you (You can test that yourself, or try to look it up.)

If not, you can use this code (Place in a public module) to create the folder first:
Expand|Select|Wrap|Line Numbers
  1. Public Function MakeDir(ByVal strPath As String) As Boolean
  2.     'creates a directory independent of whether the parent directory exists
  3.     'Code by TheSmileyOne
  4.     'Version 0.1
  5.     'Date 2010-05-19
  6.  
  7.     'Known issues
  8.     'No error handling for cases such as network drives, with restricted permissions to create folders.
  9.     'No input validation
  10.  
  11.  
  12.     On Error GoTo err_Handler
  13.  
  14.     'Check if rightmost char is a \
  15.     If Right(strPath, 1) = "\" Then
  16.         'Strip it
  17.         strPath = Left(strPath, Len(strPath) - 1)
  18.     End If
  19.  
  20.     'Check if each individual directory exists, and if not, create it
  21.     Dim strSplitPath() As String
  22.     ReDim strSplitPath(UBound(Split(strPath, "\")))
  23.     strSplitPath = Split(strPath, "\")
  24.     Dim intI As Integer
  25.     Dim strCombined As String
  26.     For intI = 0 To UBound(strSplitPath)
  27.         If intI <> 0 Then strCombined = strCombined & "\"
  28.  
  29.         strCombined = strCombined & strSplitPath(intI)
  30.         If Dir(strCombined, vbDirectory) = "" Then
  31.             MkDir strCombined
  32.         End If
  33.  
  34.  
  35.  
  36.     Next
  37.     MakeDir = True
  38. Exit Function
  39. err_Handler:
  40.     MakeDir = False
  41.         MsgBox "Error " & Err.Number & " occured." & vbNewLine & Err.Description
  42.  
  43. End Function
Your code could look something like this then:
Expand|Select|Wrap|Line Numbers
  1. Dim strFolder as string
  2. While Not rs.EOF
  3.   strFolder="z:\2012_testing\" & rs!LAST_NAME & "\"
  4.   If MakeDir(strFolder) then
  5.     DoCmd.OpenReport "rpt_Letter_A_SupportStaff", acViewPreview, , "EmpID='" & rs!EMPID & "'"
  6.     DoCmd.OutputTo acOutputReport, "", acFormatPDF, strFolder & rs!FIRST_NAME & " - " & rs!INFOYR & " Info" & ".pdf", False
  7.        DoCmd.Close acReport, "rpt_Letter_A_SupportStaff", acSaveNo
  8.   Else
  9.     Msgbox "Folder [" & strFolder & "] could not be created"
  10.   End If
  11.       rs.MoveNext
  12. Wend

10 2351
TheSmileyCoder
2,322 Expert Mod 2GB
Please confirm that you are using access 2012, I have heard of no such thing.

A few details would be nice as well. What type of records are we dealing with? Whats the table and report name? What the unique ID on which you wish to filter the report?
Oct 31 '12 #2
A12help
11
Hi. I am using Access 2012.

Here is a very scaled back version of the database; it will contain many more letter types, each with different data. The only data currently contained in the database is what I am dealing with (table, query, report and form.).

All assistance is greatly appreciated.

Thanks.
Attached Files
File Type: zip testdbfolder.zip (107.9 KB, 109 views)
Oct 31 '12 #3
zmbd
5,501 Expert Mod 4TB
Is that on the Apples?
That is not a version that I can find any information on outside of that context.

Also, unless asked for, most people here will not download an attched file from a new user... nothing personal... just precautionary.

What we need to see is just the SQL, VBA, and a detailed description of your tables with any relationships between them.

ADO vs. DAO...
Depends on what you want to do... if you'll never use a "real" SQL server such as Oracle, SQL-Server, etc.. and will be staying in the Access structure then you'll be much happier with the DAO construct. HOWEVER, if you might be upgrading or needing to connect to the other types of severs then ADO.
I use both - I just explicitly declare my connections and record sets to ensure that the correct structure is used.
Choosing ADO or DAO...
Oct 31 '12 #4
TheSmileyCoder
2,322 Expert Mod 2GB
No offense intended, but as zmbd says, I for one will not download a db from a unknown user.

Also remember it is your responsibility to declare a question in a clear and understandable manor with the details required, it is not fair to expect us to spend alot of time decoding the construct of your database. If you, the developer, are unable to describe it in a post, chances are I wont be able to guess it from looking at your db.
Oct 31 '12 #5
NeoPa
32,556 Expert Mod 16PB
Hi A12Help.

As a new user I doubt you would be aware of some of the conventions (and indeed rules) that we expect members to adhere to. These are mainly for members' own benefit, as we know from experience which types of questions get the most benefit from responders. Attachments can be requested at times, but generally a clear description of the situation is called for, to enable experts to determine whether they feel they can or want to help. I'm sure you appreciate that we have many threads to deal with every day and if every one of them required that much effort on the part of the expert just to understand what the question is then little would be achieved, and many experts would find it hard to keep any interest.

Your attitude seems fine. I just feel you need to understand the dynamics a little to realise why it's important that you, the questionner, take responsibility for formulating a clear question that we can work with.

I can certainly say that most members who appreciate this, and who have the capability of clear expression (Not all manage that unfortunately), get great help from our experts whose willingness to help is unquestioned.

There are links to the rules in various places of course, and threads in the Access Answers forum that explain how best to go about formulating a question for best results. I'll leave that with you :-)
Oct 31 '12 #6
A12help
11
Dear zmbd,

Thank you for the information on ADO vs DAO; I will keep this in mind and retain the shared link you have provided. Many thanks from my side for this. And I understand on the attachment; someone else recommended if maybe I included then it would be easier to for someone to assist me.

Dear NeoPa,

Thank you kindly for all information you have shared and the manner in doing so. I apologize to all for my lack of expertise in dealing with this; it is all very new. I fear that the expertise level found in this forum far exceeds even my ability to properly formulate the questions I have. I will reattempt though one additional time.

Access Vsn: 2012
Windows Vsn: Windows 7, 32-bit

Question: Can someone give me the code when doing an export of a multi-page report in Access 2012, to individual .pdf file format documents with each file having its own variable destination folder and sub-folder?

I have attempted to work through this in every way possible. All other code using variables works correctly except for the destination folder and sub-folder. Here is my code (again I am very new at this and just starting to learn):

Expand|Select|Wrap|Line Numbers
  1. Private Sub ctlP_LetterA_Click()
  2. Dim cn As ADODB.Connection
  3. Dim rs As ADODB.Recordset
  4. Set cn = CurrentProject.Connection
  5. Set rs = New ADODB.Recordset
  6. Dim NTWK_FOLDER As String
  7. Dim NTWK_SUBFOLDER As String
  8. Dim LAST_NAME As String
  9. Dim FIRST_NAME As String
  10. Dim FULL_NAME As String
  11. Dim INFOYR As String
  12.  
  13.  
  14. rs.Open "SELECT EmpID, LAST_NAME, FIRST_NAME, NTWK_FOLDER, NTWK_SUBFOLDER, INFOYR FROM qry_Letter_A_supportinfo;", cn, adOpenStatic, adLockPessimistic
  15. While Not rs.EOF
  16.        DoCmd.OpenReport "rpt_Letter_A_SupportStaff", acViewPreview, , "EmpID='" & rs!EMPID & "'"
  17.        DoCmd.OutputTo acOutputReport, "", acFormatPDF, "z:\2012_testing\" & rs!LAST_NAME & "," & rs!FIRST_NAME & " - " & rs!INFOYR & " Info" & ".pdf", False
  18.        DoCmd.Close acReport, "rpt_Letter_A_SupportStaff", acSaveNo
  19.       rs.MoveNext
  20. Wend
  21. End Sub
Kindest regards.
A12help


@NeoPa
Nov 1 '12 #7
TheSmileyCoder
2,322 Expert Mod 2GB
I dont know if the Docmd.OutputTo will create the directory for you (You can test that yourself, or try to look it up.)

If not, you can use this code (Place in a public module) to create the folder first:
Expand|Select|Wrap|Line Numbers
  1. Public Function MakeDir(ByVal strPath As String) As Boolean
  2.     'creates a directory independent of whether the parent directory exists
  3.     'Code by TheSmileyOne
  4.     'Version 0.1
  5.     'Date 2010-05-19
  6.  
  7.     'Known issues
  8.     'No error handling for cases such as network drives, with restricted permissions to create folders.
  9.     'No input validation
  10.  
  11.  
  12.     On Error GoTo err_Handler
  13.  
  14.     'Check if rightmost char is a \
  15.     If Right(strPath, 1) = "\" Then
  16.         'Strip it
  17.         strPath = Left(strPath, Len(strPath) - 1)
  18.     End If
  19.  
  20.     'Check if each individual directory exists, and if not, create it
  21.     Dim strSplitPath() As String
  22.     ReDim strSplitPath(UBound(Split(strPath, "\")))
  23.     strSplitPath = Split(strPath, "\")
  24.     Dim intI As Integer
  25.     Dim strCombined As String
  26.     For intI = 0 To UBound(strSplitPath)
  27.         If intI <> 0 Then strCombined = strCombined & "\"
  28.  
  29.         strCombined = strCombined & strSplitPath(intI)
  30.         If Dir(strCombined, vbDirectory) = "" Then
  31.             MkDir strCombined
  32.         End If
  33.  
  34.  
  35.  
  36.     Next
  37.     MakeDir = True
  38. Exit Function
  39. err_Handler:
  40.     MakeDir = False
  41.         MsgBox "Error " & Err.Number & " occured." & vbNewLine & Err.Description
  42.  
  43. End Function
Your code could look something like this then:
Expand|Select|Wrap|Line Numbers
  1. Dim strFolder as string
  2. While Not rs.EOF
  3.   strFolder="z:\2012_testing\" & rs!LAST_NAME & "\"
  4.   If MakeDir(strFolder) then
  5.     DoCmd.OpenReport "rpt_Letter_A_SupportStaff", acViewPreview, , "EmpID='" & rs!EMPID & "'"
  6.     DoCmd.OutputTo acOutputReport, "", acFormatPDF, strFolder & rs!FIRST_NAME & " - " & rs!INFOYR & " Info" & ".pdf", False
  7.        DoCmd.Close acReport, "rpt_Letter_A_SupportStaff", acSaveNo
  8.   Else
  9.     Msgbox "Folder [" & strFolder & "] could not be created"
  10.   End If
  11.       rs.MoveNext
  12. Wend
Nov 1 '12 #8
A12help
11
TheSmileyCoder,

My code, as I had it, would not create the directories if they did not already exist. Having them created would have been awesome but I was afraid to even think of going there yet. So you have already assisted more than I dreamed.

Thank you for covering the code I will need to make this a success! I appreciate your time (and everyone's who has looked at this).

Kindest regards,
A12help
Nov 1 '12 #9
NeoPa
32,556 Expert Mod 16PB
Excellent answer from Smiley. I'm so pleased to see such a positive result.

@A12Help - If you were at all wondering, had you not reset my post from Best Answer then I would have had to anyway. I appreciate that it was helpful to you, but it didn't deal with the question of the thread so was not an appropriate choice for that, so you did absolutely the right thing there. Nevertheless I'm very pleased you found it helpful of course :-)
Nov 2 '12 #10
A12help
11
Dear NeoPa....I understand. At the time, your's was the best answer and while very helpful, very grateful for Smiley's help which directly addressed my question/problem (once I was able to formulate it better) and thus it moved into 1st place for best answer. ;)
Nov 2 '12 #11

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

Similar topics

2
by: Ronny sigo | last post by:
Hello all, A big part of my life I have been working with databases (as a non professional) on a local machine. Apart from a small problem now and then, for which I find the solution myself, or...
0
by: Andre | last post by:
Hi, I have developed an ASP.NET application using VS 2003. My application contains folders named "images" within different sub-sections (e.g. common\images, admin\images"). The problem I have is...
35
by: Shyguy | last post by:
Is it possible, and if so how would I read and import folder names from a CD to a table? Thank you for any help.
1
by: Piyush Trivedi | last post by:
I am experiencing difficulty to access a mapped network drive in an ASP.NET application. While using the .Net framework v 1.1 implementation of System.IO.Directory.Exists() to check if the...
4
by: Jay | last post by:
What's the recommended way to change file/folder names within a project? I want to change the solution name from (say) sol1.sln to sol2.sln, and also everything else named sol1.* (eg sol1.suo,...
4
by: roblenator | last post by:
I am trying to access files on a remote windows server using the following code; my $node = 'server5'; opendir(DIR, '\\$node\C$\Servers') or warn "open failed. reason: $!"; I get an error;...
1
by: cbz9633 | last post by:
hi i want to read a folder name, after that read folder names within that folder and at last the file name and access it, that i know but i dont know how to read a folder name. please help
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
0
by: JFKJr | last post by:
Hello everyone! I am trying to export Access table data into Excel file in such a way that the table field names should be in the first line of each column in excel file followed by field data, and...
5
by: Bay0519 | last post by:
Hi, I'm using access 2003. Could someone help me list just the folder name on c:\access\ and save it into a table. Right now when I use vbdirectory, it list all my sub folders as well as the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.