Results 1 to 6 of 6
  1. #1
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370

    Question Deleting a file on the network through access

    Hello all,

    I adopted some code for having a drag and drop from outlook capability in my database.
    I have it working, basically you drop the email into a memo field and the action is caught which triggers the code to save a copy of the email as a msg to a destination folder.


    I'd like to make a button "Delete Email" that deletes the record and also deletes that msg file that was created upon dropping the email in the database.
    The file location is stored in the table after dropping the email into the database, so I'm sure this is able to be done, I'm just not sure how to go about it.
    And as I said, I got all of this code from another forum, I'm very inexperienced with coding

    Here's my code for the actual drag and drop, It may be helpful:
    Code:
    'I got the guts of this sub from Remou on tek-tips.com. S/he told me I can drag and drop an    'email to a memo field, then gave me the object control code to save the file.
        Dim olApp As Outlook.Application
        Dim olExp As Outlook.Explorer
        Dim olSel As Outlook.Selection
        Dim i, intCounter, intResponse As Integer
        Dim strFilename, strSQL, strFolderPath, strPathAndFile, strMsg As String
        Dim fs As Object
        Dim fsFolder As Object
        Dim blnFolderExists, blnFileExists As Boolean
        
        
        'My network consultant advises not putting too many files in a folder - like our Permanent Images.
        'Therefore, I will separate emails into a new folder each year. This code allows me
        'to never check on it, by creating the folder automatically when the year changes.
        Set fsFolder = CreateObject("Scripting.FileSystemObject")
        strFolderPath = "R:\Emails " & Year(Date)
        If fsFolder.FolderExists(strFolderPath) = False Then
            fsFolder.CreateFolder (strFolderPath)
        End If
    
    
        'Create the filename as a message file from the ClientID and the NoteID - which will be unique
        strFilename = [Forms]![EmailAttachmentForm]![ParticipantName] & "_" & Me![NoteID] & ".msg"
        
        'Combine for full path and file name
        strPathAndFile = strFolderPath & "\" & strFilename
        
        'Make sure this file does not already exist to avoid overwriting email files when there is a
        'system glitch.
        Set fs = CreateObject("Scripting.FileSystemObject")
        blnFileExists = fs.FileExists(strPathAndFile)
        If blnFileExists = False Then
            'There's not already a file for this client and noteID. This is the way it always
            'should be. But stuff happens. So, I'm checking.
            'Save the email to the filename just created as a message file
            Set olApp = GetObject(, "Outlook.Application")  'First argument is blank to return the currently
                                                            'active Outlook object, otherwise runtime fails
            Set olExp = olApp.ActiveExplorer
            Set olSel = olExp.Selection
            For i = 1 To olSel.Count
                olSel.Item(1).SaveAs strPathAndFile, olMSG
            Next
        Else
            'There's already a file for this client and noteID. This should be impossible,
            'but stuff happens. In this case we notify the user and then re-establish the links
            'so the user can handle it.
            strMsg = "ATTENTION: The system detected an e-mail file already created for this note. "
            strMsg = strMsg & "That e-mail is now linked to this note ID. Please do the following:" & vbCr & vbCr
            strMsg = strMsg & "1. View the e-mail normally." & vbCr
            strMsg = strMsg & "2. If it is the correct e-mail, you don't need to do anything else." & vbCr
            strMsg = strMsg & "3. If it is the wrong e-mail, use the Un-Attach E-mail button to get rid of it. "
            strMsg = strMsg & "Then attach the correct e-mail."
            MsgBox strMsg
        End If
        
        'Update the location field with the location.
        Cancel = True   'To roll back changes caused by the drop.
        Me![EmailLocation] = strPathAndFile
        Me.EmailMemo = "EMAIL ATTACHED"
        Me.EmailMemo.Locked = True
        Me.Dirty = False    'To save the changes.
        
        Set fsFolder = Nothing
        Set fs = Nothing
        Set olSel = Nothing
        Set olExp = Nothing
        Set olApp = Nothing
    DoCmd.Close

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Try:

    Kill strPathAndFile
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Getting a Run-time error '13':
    Type mismatch

    to be clear: I put "Kill strPathAndFile" in code of a button's OnClick event.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Did you declare and set the strPathAndFile variable?

    Maybe you just need to refer to field and pull path/filename value from the form current record.

    Kill Me!fieldname

    However, if the file doesn't exist, the code will error. First test if file exists.

    If Dir(Me!fieldname) <> "" Then Kill Me!fieldname
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Quote Originally Posted by June7 View Post
    Did you declare and set the strPathAndFile variable?
    Where am I declaring it? In the code I provided above? Or in my OnClick event of the button?

    Quote Originally Posted by June7 View Post
    If Dir(Me!fieldname) <> "" Then Kill Me!fieldname
    I tried this and it does nothing.

  6. #6
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Ok I got it to work, but also how do i delete that record? Normally I just use a delete record button, but I want this button to be all in one, to delete the record and the file

    EDIT: Got it!
    Last edited by cbende2; 01-06-2015 at 02:49 PM. Reason: Figured it out

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 05-25-2014, 11:16 PM
  2. Deleting a non-Access file
    By rbtrout in forum Access
    Replies: 2
    Last Post: 05-23-2011, 12:11 PM
  3. Deleting a file
    By fiamma68 in forum Access
    Replies: 3
    Last Post: 11-07-2010, 06:10 PM
  4. Replies: 0
    Last Post: 08-20-2008, 10:14 PM
  5. Replies: 2
    Last Post: 01-14-2008, 12:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums