Vous êtes sur la page 1sur 6

Private Sub cmdAdd_Click()

On Error GoTo SubError


Dim rsParent As DAO.Recordset
Dim rsAttachment As DAO.Recordset2
Dim strFileName As String
Dim SQL As String
'Add "Microsoft Office 14.0 Object Library" in references
Dim fDialog As Office.FileDialog
Dim varFile As Variant

'Get the file to upload


Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Title = "Choose the document to add to the form..."
.AllowMultiSelect = False
.InitialFileName = "C:\Users\ray\Desktop\AccessJitsu Dbs"

If .Show = True Then


If .SelectedItems.count = 0 Then
'User clicked open but didn't select a file
GoTo SubExit
End If

For Each varFile In .SelectedItems


strFileName = varFile
Next
Else
GoTo SubExit
End If
End With

'find the record in the attachments table we are currently on


SQL = "SELECT * FROM Attachments WHERE RecordID = " & Me.txtRecordID
Set rsParent = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)

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

Private Sub cmdDelete_Click()


On Error GoTo SubError
Dim rsParent As DAO.Recordset
Dim rsAttachment As DAO.Recordset2
Dim SQL As String
Dim strFileName As String

strFileName = Form_subfrmAttachmentData2.txtFileName

If MsgBox("Are you sure you want to delete " & strFileName & "?", vbQuestion +
vbYesNo, _
"Delete file?") = vbNo Then
Exit Sub
End If

'find the record in the attachments table we are currently on


SQL = "SELECT Document FROM Attachments WHERE RecordID = " & Me.txtRecordID
Set rsParent = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)

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

'Save button click


' Pick the folder to save document in
' Call SaveToFile function
Private Sub cmdSave_Click()
On Error GoTo SubError
Dim strPath As String
'Add "Microsoft Office 14.0 Object Library" in references
Dim fDialog As Office.FileDialog
Dim varFile As Variant

'Where to save document...


Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Choose the folder to save the document in..."
.AllowMultiSelect = False
.InitialFileName = "C:\Users\ray\Desktop\AccessJitsu Dbs\" 'Folder picker
needs trailing slash

If .Show = True Then


If .SelectedItems.count = 0 Then
'User clicked open but didn't select a file
GoTo SubExit
End If

For Each varFile In .SelectedItems


strPath = varFile
'Get key data and FileName of attachment to save
'Debug.Print strPath
'Debug.Print Me.txtRecordID
'Debug.Print Form_subfrmAttachmentData.txtFileName
If SaveAttToFile(Me.txtRecordID, strPath,
Form_subfrmAttachmentData2.txtFileName) = True Then
MsgBox "File saved!", vbInformation + vbOKOnly, "Success"
Else
MsgBox "There was an error, the file was not saved.",
vbCritical + vbOKOnly, "Error"
End If
Next
Else
GoTo SubExit
End If
End With

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

'find the record in the attachments table we are currently on


SQL = "SELECT Document FROM Attachments WHERE RecordID = " & RecordID
Set rsParent = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)

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

Dim fDialog As Office.FileDialog


Dim varFile As Variant
Dim rsFile As DAO.Recordset
Dim strFilePath As String, strFilename As String
Dim sql As String

' Set up the File Dialog. '


Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

With fDialog

' Allow user to make multiple selections in dialog box '


.AllowMultiSelect = False

' Set the title of the dialog box. '


.Title = "ODABERITE SLIKU, LOGO, AMBLEM, MEMORANDUM"

' 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

Vous aimerez peut-être aussi