Private Sub cmdAttach_Click()
'On Error GoTo errline
Dim db As dao.Database
Dim rs As dao.Recordset
Dim LastSlash As Integer
Dim fname As String
Dim DestinationPathAndName As String
Dim strFolder As String
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_documents", dbOpenDynaset, dbSeeChanges, dbOptimistic)
If Not IsNull(Me.document_path) Or Not IsNull(Me.file_type) Then
LastSlash = InStrRev(Me.document_path, "\") 'the location of the last slash before the file name
fname = Mid(Me.document_path, LastSlash + 1) 'the file name without path
DestinationPathAndName = GBLnetworkStoragePath & "\" & "documents"
If Len(Dir(GBLnetworkStoragePath & "\" & "documents", vbDirectory)) > 0 Then
'Directory Exists
Else
'Create the folder
MkDir GBLnetworkStoragePath & "\" & "documents"
End If
CheckAgain:
'check if the file name exists, before copying the new file
If Dir(DestinationPathAndName & "\" & fname) <> "" Then
'the exists, so let's ask the user to
AhEmptyName:
fname = InputBox("Please enter a new file name. You must include the file extension.")
If Len(Trim(fname)) = 0 Then
MsgBox "Please enter a file name"
GoTo AhEmptyName
End If
'let's check again on the new file name existance
GoTo CheckAgain
Else
'does not exists, so let the code flow to continue
End If
FileCopy Me.document_path, DestinationPathAndName & "\" & fname 'copy the file to the new destination
rs.AddNew
rs("document_desc") = Me.document_desc
rs("company_id") = Me.company_id
rs("file_type") = Me.file_type
rs("attachment") = Me.chkAttachment
rs("document_path") = DestinationPathAndName & "\" & fname 'assign the new location to the Table
rs.Update
Else
MsgBox "You need to browse for a document and select a file type.", vbCritical, "Error"
Me.document_desc.SetFocus
End If
rs.Close
db.Close
MsgBox "Document has been saved to this company contact.", vbInformation, "Attach Document"
Set rs = Nothing
Set db = Nothing
DoCmd.Close
exitline:
Exit Sub
errline:
Select Case Err.Number
Case 94
MsgBox "There are blank fields", vbExclamation, "Error..."
Case 2450
MsgBox "ContactPLUS needs to restart", vbExclamation, "Error..."
Call Restart
Case Else
MsgBox "An error has occured. Please notify the Database Administrator of the following error number:" & Err.Number & vbCrLf & "The error message is: " & Err.Description
GoTo exitline
End Select
End Sub
|