Hi All
I'm creating a report for each dog which I need to save in each dog's folder eg. C:\Dogs\DogID\Documents.
My code does not create the directory and I, therefore, get a run-time error '5' Invalid procedure call or argument on the following line
DoCmd.OutputTo acOutputReport, "rptCouncilNotification", acFormatPDF, NewFilePath & FileName
My other problem is the SQL Update which does not update the checkbox.
strSQL = "UPDATE tblDogsChecklist SET CouncilNotified = True" WHERE DogID = " & DogID & ";"
Where do I go wrong?
I'm creating a report for each dog which I need to save in each dog's folder eg. C:\Dogs\DogID\Documents.
My code does not create the directory and I, therefore, get a run-time error '5' Invalid procedure call or argument on the following line
DoCmd.OutputTo acOutputReport, "rptCouncilNotification", acFormatPDF, NewFilePath & FileName
My other problem is the SQL Update which does not update the checkbox.
strSQL = "UPDATE tblDogsChecklist SET CouncilNotified = True" WHERE DogID = " & DogID & ";"
Where do I go wrong?
Code:
Private Sub CmdReports_Click()
Dim strSQL As String
Dim i As Long
Dim DocumentTypeID As Long
Dim DogID As Long
Dim NewFilePath As String
Dim FileName As String
Dim fso As Object
If Forms!frmCouncilgrouplistbox!ContactList.ItemsSelected.Count < 1 Then
Exit Sub
End If
For i = 0 To Forms!frmCouncilgrouplistbox!ContactList.ListCount - 1
If Forms!frmCouncilgrouplistbox!ContactList.Selected(i) Then
Call FolderExistsCreate("C:\Dogs\" & DogID & "\Documents", True)
DogID = Forms!frmCouncilgrouplistbox!ContactList.Column(0, i)
NewFilePath = "C:\Dogs\" & DogID & "\Documents"
FileName = "CouncilNotification.pdf"
'Create the Reports
DoCmd.OpenReport "rptCouncilNotification", acViewPreview, , "DogID=" & Forms!frmCouncilgrouplistbox!ContactList.Column(0, i), acWindowNormal
DoCmd.OutputTo acOutputReport, "rptCouncilNotification", acFormatPDF, NewFilePath & FileName
DoCmd.SetWarnings False
DocumentTypeID = 9
DoCmd.RunSQL "INSERT INTO tblDocument (DogID, Filename, NewFilePath, DocumentTypeID) " & _
"VALUES (" & DogID & ", """ & FileName & """,""" & NewFilePath & """," & DocumentTypeID & ")"
DoCmd.SetWarnings True
'Set fso = CreateObject("Scripting.FileSystemObject")
'CopyFile FilePath, NewFilePath
'If Not FileExists(NewFilePath) Then
' MsgBox "ERROR: File Copy Failed. " & NewFilePath
' Exit Sub
'End If
'Set fso = Nothing
End If
DoCmd.SetWarnings False
DocumentTypeID = 9
strSQL = "INSERT INTO tblDocument (DogID, Filename, NewFilePath, DocumentTypeID) " & _
"VALUES (" & DogID & ", """ & FileName & """," & FilePath & """," & DocumentTypeID & ");"
Debug.Print strSQL
strSQL = "UPDATE tblDogsChecklist SET CouncilNotified = True WHERE DogID = " & DogID & ";"
Debug.Print strSQL
DoCmd.SetWarnings True
Next
End Sub
Function FolderExistsCreate(DirectoryPath As String, CreateIfNot As Boolean) As Boolean
Dim Exists As Boolean
On Error GoTo DoesNotExist
Exists = ((GetAttr(DirectoryPath) And vbDirectory) = vbDirectory)
If Exists Then
FolderExistsCreate = True
Else
' Doesn't Exist Determine If user Wants to create
If CreateIfNot Then
MkDir DirectoryPath
FolderExistsCreate = True
Else
FolderExistsCreate = False
End If
End If
Exit Function
DoesNotExist:
FolderExistsCreate = False
End Function