ID code help to save current record

crann

Registered User.
Local time
Today, 13:21
Joined
Nov 23, 2002
Messages
160
Hi

Can anyone help with this.

I currently use the following code to save a report as a pdf to my desktop. that all works fine but I need to somehow make sure it saves the report for the record I am on in the form. At the moment it just saves the first record.

I do have ID fields on form and report but dont know what code to use to ensure it saves the current record.

I was kindly given this code as beginner.

Thanks


Code:
Dim FileName As String
Dim FilePath As String
Dim UserLogin As String
On Error GoTo My_ErrorHandler:

UserLogin = Environ("UserName")
FileName = "DevelopmentPack"

FilePath = "C:\Users\Jonathan\Desktop\" & FileName & ".pdf"

DoCmd.OutputTo acOutputReport, "rptMainReport", acFormatPDF, FilePath

MsgBox "Development Pack has been successfully saved as a PDF to Desktop", vbInformation, "Save confirmed"

exit_My_ErrorHandler:
    Exit Sub
My_ErrorHandler:
    MsgBox "Error # :" & err.Number & ", " & err.Description, vbExclamation
    Resume Next
 
append the id to the filename that will give you a clue:

FilePath = "C:\Users\Jonathan\Desktop\" & FileName & "_" & Me!ID & ".pdf"
 
Hi

Sorry little bit lost there.

I have replaced that line of code but still saves the pdf report with all the records on not the specific record I am on. was I suppose to do something else?

Thanks
 
if your report is based on query, copy and saved it in another name, say if the query name is Query1, copy it to Query2.

modify Query2 and set a Criteria on ID field:
Forms!yourFormName!ID

next create a copy of your report. if your report name is Report1, copy it and named it to something else,ie Report2.
modify the Data of your Report2 and put in our new query as its Recordsource.

modify your code, and replace this part:

DoCmd.OutputTo acOutputReport, "rptMainReport", acFormatPDF, FilePath

with the report that we have modified:

DoCmd.OutputTo acOutputReport, "Report2", acFormatPDF, FilePath

also reinstate this:

FilePath = "C:\Users\Jonathan\Desktop\" & FileName & ".pdf"
 
Brilliant, works great.

Really helpful!

To complete this process I want the pdf to be saved with the file name as detailed in the code "Development Pack" plus the field from the form "Projectname"

The PDF file should read something like: Development Pack (Project Name)

That way each record saved inclused its project name from the form.

Can this be done or do I need to always have a fixed File Name?

Thanks
 
you need to fix the filename:

FileName = "DevelopmentPack (" & Forms!ProjectName!ControlName & ")"

just replace the ControlName with the correct control in ProjectName form.
 
Thanks so much exactly what I was trying to achieve.

I had been trying to write that bit of code myself but for some reason I wasn't using the form name I assumed for some daft reason it would automatically use the current form.

Thanks again for your support
 

Users who are viewing this thread

Back
Top Bottom