Help with creating a button to export PDF file

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 🥲
 
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
 
I wad thinking of a simple MkDir ?
 
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
 
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

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
 
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 😣
 
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. :(
 
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?
 
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.
 
Just print to PDF after the Print preview regardless.
 

Users who are viewing this thread

Back
Top Bottom