Académique Documents
Professionnel Documents
Culture Documents
If rsParent.RecordCount = 0 Then
MsgBox "There was a problem locating the selected record", vbCritical +
vbOKOnly, "Error"
GoTo SubExit
Else
'Put recordset in edit mode
rsParent.Edit
'Set the child recordset
Set rsAttachment = rsParent!Document.Value
'Add a new record
rsAttachment.AddNew
rsAttachment.Fields("FileData").LoadFromFile strFileName
'Save changes
rsAttachment.Update
rsParent.Update
'Update the sub-form
Form_subfrmAttachmentData2.Requery
End If
SubExit:
On Error Resume Next
If Not rsParent Is Nothing Then
rsParent.Close
Set rsParent = Nothing
End If
Exit Sub
SubError:
MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical +
vbOKOnly, _
"An error occurred"
GoTo SubExit
End Sub
strFileName = Form_subfrmAttachmentData2.txtFileName
If MsgBox("Are you sure you want to delete " & strFileName & "?", vbQuestion +
vbYesNo, _
"Delete file?") = vbNo Then
Exit Sub
End If
If rsParent.RecordCount = 0 Then
MsgBox "There was a problem locating the selected record", _
vbCritical + vbOKOnly, "Error"
GoTo SubExit
Else
Set rsAttachment = rsParent!Document.Value
If rsAttachment.RecordCount <> 0 Then
rsAttachment.FindFirst "FileName='" & strFileName & "'"
If rsAttachment.NoMatch Then
MsgBox "There was a problem locating the attached document", _
vbCritical + vbOKOnly, "Error"
GoTo SubExit
Else
rsAttachment.Delete
Form_subfrmAttachmentData2.Requery
End If
End If
End If
SubExit:
On Error Resume Next
If Not rsParent Is Nothing Then
rsParent.Close
Set rsParent = Nothing
End If
Exit Sub
NoDocFound:
MsgBox "No document found in attachment", vbCritical + vbOKOnly, _
"An error occurred"
GoTo SubExit
SubError:
MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical +
vbOKOnly, _
"An error occurred"
GoTo SubExit
End Sub
SubExit:
On Error Resume Next
Set fDialog = Nothing
Exit Sub
SubError:
MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical +
vbOKOnly, _
"An error occurred"
GoTo SubExit
End Sub'**********************************************************************
'FUNCTION:
' Finds the attachment passed via parameter and saves it to a file
'RETURNS:
' Returns true if the file was successfully saved
'ARGUMENTS:
' RecordID of parent record
' Full file path to save document to
' FileName of attachment to retrieve
'ASSUMPTIONS:
' Save path exists (caller must assure this)
' Will match to the first occurrence of FileName passed to it
'**********************************************************************
Public Function SaveAttToFile(RecordID As Long, FilePath As String, FileName As
String) As Boolean
On Error GoTo SubError
Dim rsParent As DAO.Recordset
Dim rsAttachment As DAO.Recordset2
Dim SQL As String
Dim strFileName As String
SaveAttToFile = False
If rsParent.RecordCount = 0 Then
GoTo NoDocFound
Else
Set rsAttachment = rsParent!Document.Value
If rsAttachment.RecordCount <> 0 Then
rsAttachment.FindFirst "FileName='" & FileName & "'"
If rsAttachment.NoMatch Then
GoTo NoDocFound
Else
strFileName = FilePath & "\" & rsAttachment!FileName
'make sure this file doesn't already exist!
If Dir(strFileName) <> "" Then
FileSystem.Kill strFileName
End If
'Save the document to a file
rsAttachment!FileData.SaveToFile strFileName
SaveAttToFile = True
End If
Else
GoTo NoDocFound
End If
End If
SubExit:
On Error Resume Next
If Not rsParent Is Nothing Then
rsParent.Close
Set rsParent = Nothing
End If
Exit Function
NoDocFound:
MsgBox "No document found in attachment", vbCritical + vbOKOnly, _
"An error occurred"
GoTo SubExit
SubError:
MsgBox "Error Number: " & Err.Number & " = " & Err.Description, vbCritical +
vbOKOnly, _
"An error occurred"
GoTo SubExit
End Function
With fDialog
' Clear out the current filters, and add our own.'
.Filters.Clear
.Filters.Add "Graphics", "*.jpg, *.jpeg, *.gif"
.Filters.Add "All Files", "*.*"
.ButtonName = "Select"
' Show the dialog box. If the .Show method returns True, the '
' user picked at least one file. If the .Show method returns '
' False, the user clicked Cancel. '
If .Show = True Then
'Loop through each file selected and add it to our list box. '
For Each varFile In .SelectedItems
strFilePath = varFile
If strFilePath & "" <> "" Then
'strFilename = Mid(strFilePath, InStrRev(strFilePath, "\") + 1)
'find the record in the attachments table we are currently on
'sql = "SELECT * FROM HEADER WHERE [ORGAN] = '" & Me.TXTORGAN & "'"
With CurrentDb.OpenRecordset("HEADER")
.AddNew
!SLIKA = strFilePath
!ORGAN = Me.TXTORGAN
!HEADER = Me.TXTHEADER
.Update
End With
End If
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With