Hello all,
I have used a lot of code from various posts on here and it has been very helpful and has got me 99% of the way where I want to be. My script does the following:
If there are multiple records in the table, the PDF step saves all of the records one after another in one PDF. I would like for the PDF function to only write the current record as the PDF, but still save all of the attachments contained in that single record.
I'm sure it has something to do with the loop I found, but I can't seem to come up with a way to make sure the only content the PDF creates is in the current record.
The following is the code.
Help with this would be extremely appreciated as I have spent 4+ hours trying to figure this one out. I will answer any questions you have to help solve the problem.
I have used a lot of code from various posts on here and it has been very helpful and has got me 99% of the way where I want to be. My script does the following:
- Sets Variables and directories.
- Creates folders
- Creates a PDF from the current form
- Saves attachments to a folder
If there are multiple records in the table, the PDF step saves all of the records one after another in one PDF. I would like for the PDF function to only write the current record as the PDF, but still save all of the attachments contained in that single record.
I'm sure it has something to do with the loop I found, but I can't seem to come up with a way to make sure the only content the PDF creates is in the current record.
The following is the code.
Code:
Private Sub Save_Gaurdrail_Hit_Click()
Dim MyFilter As String
Dim MyPath As String
Dim MyFilename As String
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CreateFolder "T:\Trinity Guardrail\" + Me.Major_Route & Space(1) + Me.Maintenance_Jurisdiction & " - MM " + Me.County___State_Milepoint
MyFilename = Me.Major_Route & Space(1) + Me.Maintenance_Jurisdiction & " - " + Me.County___State_Milepoint
MyPath = "T:\Trinity Guardrail\" + Me.Major_Route & Space(1) + Me.Maintenance_Jurisdiction & " - MM " + Me.County___State_Milepoint
'Creates Attachements Folder'
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CreateFolder MyPath + "\" + "Attachments"
'Creates PDF of form'
DoCmd.OutputTo acOutputForm, "Guardrail Hit", acFormatPDF, MyPath + "\" + MyFilename + ".pdf", False, , , acExportQualityScreen
Application.FollowHyperlink MyPath
' Exports all attachments in Guardrail Hit
On Error GoTo Err_SaveImage
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = Me.Recordset
rsParent.OpenRecordset
Set rsChild = rsParent.Fields("Attachments").Value
With rsChild
Do Until .EOF
.MoveFirst
rsChild.OpenRecordset
rsChild.Fields("FileData").SaveToFile (MyPath + "\" + "Attachments")
rsChild.Delete
Me.Refresh
.MoveNext
Loop
.Close
MsgBox "Complete"
End With
Exit_SaveImage:
Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub
Err_SaveImage:
If Err = 3839 Then
MsgBox ("File Already Exists in the Directory!")
Resume Next
Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_SaveImage
End If
End Sub
Help with this would be extremely appreciated as I have spent 4+ hours trying to figure this one out. I will answer any questions you have to help solve the problem.