Results 1 to 3 of 3

RunTime error 70 on Kill(Filepath & Filename)

Threaded View

Kramxel RunTime error 70 on... 11-13-2017, 09:42 AM
Kenneth Hobson Re: RunTime error 70 on... 11-13-2017, 10:27 AM
Kramxel Re: RunTime error 70 on... 11-13-2017, 11:03 AM
  1. #1
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    RunTime error 70 on Kill(Filepath & Filename)

    Hi All,

    Having an issue I'm struggling (read as 'failing miserably') to solve.

    Initially I was connecting to a workbook via an ODA connection - but occasionally this file maybe in use by another machine which due an MS bug/memory leak will open a read-only copy of the file. To avoid this I have created a sub which creates a copy of the file in a temp location runs the connection string and then attempts to delete the file again. When doing this I run into run time error 70 at the Kill command after some research I guess this is because there is still a connection to the file and it appears open. If I change the code so that it kills the file first (as in a copy was created from a previous process) and then hold F5 the code cycles through without problem - I guess the End Sub is breaking the link each time so that the kill command from the next loop through the sub fires. I have tried adding Reset and Close to the MakeCopy function but this doesn't seem to help either.

    in short my question is how can I break the connection earlier so I do not have to leave a file needlessly until the next time.

    Sub GetAgentManager()
        Dim sSQLQry As String
        Dim ReturnArray, output As Variant
        Dim Conn As New ADODB.Connection
        Dim mrs As New ADODB.Recordset
        Dim DBFilePath, DBFileName, DBPath, sconnect As String
        Dim i, j, k As Integer
        
        With Worksheets("Lists")
            i = Application.WorksheetFunction.Match("AGENTS", .Range("A:A")) + 1
            j = i + Application.WorksheetFunction.CountA(.Range("A:A")) - 3
            .Range(.Cells(i, 1), .Cells(j, 2)).ClearContents
        End With
        
        
        DBFilePath = [rFilePath]
        DBFileName = [rFileName]
            
        DBPath = MakeCopy(CStr(DBFilePath), CStr(DBFileName))
        
        sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
           
        Conn.Open sconnect
        sSQLString = "SELECT Agent_Name, Team_Manager From [AgentData$]"
      
        mrs.Open sSQLString, Conn
        
        ReturnArray = mrs.GetRows
    
        mrs.Close
        
        Conn.Close
        ReDim output(0 To UBound(ReturnArray, 2), 0 To UBound(ReturnArray, 1))
        For i = 0 To UBound(output, 1)
            output(i, 0) = ReturnArray(0, i)
            output(i, 1) = ReturnArray(1, i)
        Next i
        
        Worksheets("Lists").Range("A11").Resize(UBound(output, 1) + 1, UBound(output, 2) + 1).Value = output
        DoEvents
        Kill (DBPath)
    End Sub
    
    Function MakeCopy(sFilePath As String, sFileName As String)
        Dim sTempDir As String, sSource As String
        Dim fso As Object
        Set fso = CreateObject("scripting.FileSystemObject")
        
        
        sTempDir = Environ$("temp") & "\" & sFileName
        'Kill (sTempDir) 'if I comment out Kill command from sub above and uncomment this one - code loops without issue
        sSource = sFilePath & "\" & sFileName
        fso.CopyFile sSource, sTempDir, True
        MakeCopy = sTempDir
        
        sFilePath = Empty
        sFileName = Empty
        sTempDir = Empty
        sSource = Empty
        Set fso = Nothing
    End Function
    *EDIT* if I declare DBPAth as a public string and wrap the first sub in another which then runs the kill command after the end sub of the code ie.

    sub One
       call GetAgentManager
       Kill (DBPath)
    End sub
    it works perfectly, but seems a bit of a clunky workaround - really must be missing something a bit slicker/simpler.
    Last edited by Kramxel; 11-13-2017 at 10:08 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Kill command & Path/File access error (Error 75)
    By bongaan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2021, 08:21 PM
  2. Save sheet as pdf with cell-based filename in a specific filepath
    By CoachK88 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-16-2017, 05:43 PM
  3. Trim filename from filepath using VBA with range in specific column
    By isritter in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-20-2014, 05:42 PM
  4. [SOLVED] SaveAs and Kill filename
    By milo1984 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2014, 02:08 PM
  5. [SOLVED] Runtime 1004 after prompting for filename but not when filename hard coded.
    By cgoodenough in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-04-2013, 03:46 AM
  6. Export to HTML filename and filepath
    By magicool in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2009, 05:33 PM
  7. How to split filename from filepath?
    By Frank in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-04-2005, 02:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1