Help with creating a button to export PDF file (1 Viewer)

113hans

Member
Local time
Today, 19:21
Joined
Mar 31, 2021
Messages
47
here try this, it will ask for a folder (or you can create new folder).
Actually, I will name the new folder after the name of the record in the field "Hoten", each new folder equals each record
so, if I create a new folder, I have to type the content of the record in the field again
I mean if the record is "Nguyễn Thị Vân", I have to type "Nguyễn Thị Vân" for the new folder
does it have a code to automatically create a new folder named after the record? No need to ask Y/N.
Hope you have a solution 🥲
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:21
Joined
Feb 19, 2002
Messages
43,275
You would use FSO (File System Object) to create folders. Open any code module and go to Tools/References. Select a reference to
Microsoft Scripting Runtime
Code:
'''' needs reference to Microsoft Scripting Runtime

    Dim fs          As Scripting.FileSystemObject

    Set fs = CreateObject("Scripting.FileSystemObject")
    NewFolderName = DLookup("ScannedDocPath", "tblAuditParms", "CoAbbr = '" & sCoAbbr & "'")
    'create folder for coabbr if necessary
    If fs.FolderExists(NewFolderName) Then
    Else
       fs.CreateFolder (NewFolderName)
    End If
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:21
Joined
Sep 21, 2011
Messages
14,305
I wad thinking of a simple MkDir ?
 

113hans

Member
Local time
Today, 19:21
Joined
Mar 31, 2021
Messages
47
You would use FSO (File System Object) to create folders. Open any code module and go to Tools/References. Select a reference to
Microsoft Scripting Runtime
Code:
'''' needs reference to Microsoft Scripting Runtime

    Dim fs          As Scripting.FileSystemObject

    Set fs = CreateObject("Scripting.FileSystemObject")
    NewFolderName = DLookup("ScannedDocPath", "tblAuditParms", "CoAbbr = '" & sCoAbbr & "'")
    'create folder for coabbr if necessary
    If fs.FolderExists(NewFolderName) Then
    Else
       fs.CreateFolder (NewFolderName)
    End If
Your code is very technical for an access newbie like me.
I don't know which part that I should replace my own info.
ScannedDocPath??? tblAuditParm??? ... o_O
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:21
Joined
May 7, 2009
Messages
19,245
try this again it will ask which folder to put the pdf.
then it will create new folder on the chosen folder (with same name as the pdf).
 

Attachments

  • Button to create PDF file nondatecode.accdb
    2.6 MB · Views: 51

Gasman

Enthusiastic Amateur
Local time
Today, 13:21
Joined
Sep 21, 2011
Messages
14,305
Sorry, I don't understand much of what you said. I'm still a newbie 😖
You wanted to create the name and folder perhaps from values on the form?, not have to respond to each pdf file being created.

Where I have happened to hard code Gazette, you use a control value.?
The filename is the name of the ship, in my case.
Code:
Sub Print_All_Ships()
 Dim rs As DAO.Recordset
 Dim db As Database
 Dim stSQL As String, stDate As String, stDBpath As String, stFTPpath As String
 Dim stRptName As String, stParam As String, stLinkCriteria As String, stAlphabet As String, astAlpa(1, 26) As String
 Dim stStart As String, stEnd As String, iloop As Integer
 Dim iOKCancel As Integer
  
 ' GoTo rptalpha
  
 stRptName = "Main_by_Ship"
 Set db = CurrentDb
' Generate all the Ship reports
' GoTo rptleave:
stDBpath = CurrentProject.Path & "\"
stFTPpath = stDBpath & "Gazette\"

 stSQL = "SELECT Ship.Ship FROM Ship WHERE (((Ship.ID)<> 26 and (Ship.ID)<> 27 and (Ship.ID)<> 60))ORDER BY Ship.Ship"

 Set rs = db.OpenRecordset(stSQL)

 Do While Not rs.EOF
 ' Need to convert any spaces in ship name to _ for website
    stParam = LCase(Replace(rs!Ship, " ", "_"))
    stLinkCriteria = "[Ship] = '" & rs!Ship & "'"
    
    'DoCmd.CopyObject , stParam, acReport, stRptName
    DoCmd.OpenReport stRptName, acViewPreview, , stLinkCriteria, acHidden
 ' Pause for 5 seconds to save report
    'Pause (5)
    DoCmd.OutputTo acOutputReport, stRptName, acFormatPDF, stFTPpath & stParam & ".pdf", False
    DoCmd.Close acReport, stRptName
    'DoCmd.DeleteObject acReport, stParam

    rs.MoveNext
    
'    iOKCancel = MsgBox("OK to proceed?", vbOKCancel)
'    If iOKCancel = vbCancel Then
'        Exit Sub
'    End If
    
 Loop
 rs.Close
End Sub
 

113hans

Member
Local time
Today, 19:21
Joined
Mar 31, 2021
Messages
47
You wanted to create the name and folder perhaps from values on the form?, not have to respond to each pdf file being created.

Where I have happened to hard code Gazette, you use a control value.?
The filename is the name of the ship, in my case.
Any code which has more than 5 lines is considered to be too complicated to me.
I really don't know how to explain for you, truly sorry, I'm so noob 😣
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:21
Joined
Sep 21, 2011
Messages
14,305
Any code which has more than 5 lines is considered to be too complicated to me.
I really don't know how to explain for you, truly sorry, I'm so noob 😣
Best take up another hobby then, or take the time to learn. :(
It is only going to get harder. :)

All that code is only beaking each part down into steps, so I can get the path, the parameter and anything else I need to print the report to the correct place with the correct name.

You have to put some effort in yourself. If you just get code handed to you on a plate, you will never learn anything. :(
 

113hans

Member
Local time
Today, 19:21
Joined
Mar 31, 2021
Messages
47
I have a difficult question I think.
Here my move: In the report, I click Print preview. In Print preview window, I select Print and it give me Print window to work with.
The picture I attached is the Print window.

Untitled.png


In the picture, the green rectangle includes choices for printer, the red rectangle contains options for PDF file.
I wonder can we mix the 2 options in one, I mean when I choose a printer (e.g. EPSON T60 Series) and click OK, it will print a report on the printer, and it will also automatically print a PDF file for that report and save it on a folder.
A command button will get this easily I know, but If I want to do the print this way, can the problem be solved?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:21
Joined
Feb 19, 2002
Messages
43,275
Not using that dialog. You need to create your own button and your own code. You can't change the way a windows dialog works.

But doing what you want is trivial. You just have two lines of code for the report. One that uses the OpenReport method to print it directly to the printer. The second line uses the OutputTo method to export the file to a PDF and save it.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:21
Joined
Sep 21, 2011
Messages
14,305
Just print to PDF after the Print preview regardless.
 

Users who are viewing this thread

Top Bottom