UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Kill Not Working, Error 70, Access 2013    
 
   
ermonyi
post Dec 23 2017, 02:01 PM
Post#1



Posts: 19
Joined: 7-January 06



Hi all.

I want to delete a file (after checking if it exists) with Kill, but I get error 70 (permission denied).
The file exists and I can delete it manually, but not via VBA.

With this function I check if the file exists (and it works):
CODE
Function fExisteArchivo(strProcLlamante As String, strArchivo As String) As Boolean
On Error GoTo Err_fExisteArchivo

    If Len(Dir(strArchivo)) > 0 Then
        fExisteArchivo = True
    Else
        fExisteArchivo = False
    End If

done:
    Exit Function
Err_fExisteArchivo:
    Select Case Err.Number
        Case 52 'Nombre o numero de archivo incorrecto
        
        Case Else
            fExisteArchivo = False
            fErrores True, Err.Number, "Módulo: modArchivos. Función: fExisteArchivo. Procedimiento: " & strProcLlamante & vbCrLf & vbCrLf & "Archivo: " & strArchivo, Err.Description
    End Select
End Function

fErrores is an error handler function to insert error data in a table.

With this code I try to kill the file:
CODE
    If fExisteArchivo("fSalir", Application.CurrentProject.Path & "\" & ArchivoAyuda) = True Then
        Kill Application.CurrentProject.Path & "\" & ArchivoAyuda
    End If


If I check this in the inmediate window I get:
?fExisteArchivo("fSalir", Application.CurrentProject.Path & "\" & ArchivoAyuda) = True (The file exists).
?Application.CurrentProject.Path & "\" & ArchivoAyuda = C:\Users\Propio\Documents\BDs\Ayuda Personal.chm (The full path is right).

But Kill doesn't works iconfused.gif

Any suggestion.
Thanks in advance.

--------------------
Regards from Sevilla (España)
Go to the top of the page
 
theDBguy
post Dec 23 2017, 02:44 PM
Post#2


Access Wiki and Forums Moderator
Posts: 71,915
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Would you be trying to kill an open file?

Just thinking out loud...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
ermonyi
post Dec 23 2017, 03:03 PM
Post#3



Posts: 19
Joined: 7-January 06



Hi, as far as I can see the file is not open.
In my tests I close the file manually.

When the user presses F1 the file is copied fron the BackEnd server to the Application.CurrentProject.Path (because a .chm file must be open from a local drive), and when the user closes the DataBase the file is not open (In that case I could add a few code lines to order the user to close the file).

Is it possible to force/check the file closing fron VBA?

Thanks.

EDIT:

I force the file closing with this code, but the Kill doesn't works.

CODE
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const SC_CLOSE = &HF060&
Private Const WM_SYSCOMMAND = &H112


Public Function fCierraArchivo(strArchivo As String) 'Cierra la aplicacion por el titulo de la ventana
On Error Resume Next
    Dim ClassName As String
    Dim hWnd As Long
    hWnd = FindWindow(ClassName, strArchivo)
    Call SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, ByVal 0&)
End Function


This post has been edited by ermonyi: Dec 23 2017, 03:23 PM

--------------------
Regards from Sevilla (España)
Go to the top of the page
 
ermonyi
post Dec 23 2017, 04:05 PM
Post#4



Posts: 19
Joined: 7-January 06



Hi.

With this code I try to check if the file is open:
CODE
Public Function fIsFileOpen(strArchivo As String) As Boolean
' This function checks to see if a file is open or not. If the file is already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is some other problem accessing the file.

On Error Resume Next   ' Turn error checking off.
    Dim filenum As Integer, errnum As Integer
    filenum = FreeFile()   ' Get a free file number.
    
    ' Attempt to open the file and lock it.
    Open strArchivo For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.

On Error GoTo 0        ' Turn error checking back on.
    Select Case errnum
        ' No error occurred. File is NOT already open by another user.
        Case 0
            fIsFileOpen = False
        ' Error number for "Permission Denied". File is already opened by another user.
        Case 70
            fIsFileOpen = True
        ' Another error occurred.
        Case Else
            Error errnum
    End Select
End Function


If the .chm file is open, the function returns True (OK), but if I close the .chm file manually and I call the function again, it keeps returning True!!!

If I close the DataBase and open it (with the chm file not open), the same function returns False. iconfused.gif

Is in certain mode the file open/hidden (after open and close it from de DataBase) while the DataBase is open??

--------------------
Regards from Sevilla (España)
Go to the top of the page
 
theDBguy
post Dec 23 2017, 04:38 PM
Post#5


Access Wiki and Forums Moderator
Posts: 71,915
Joined: 19-June 07
From: SunnySandyEggo


When the function returns True to indicate the file is Open, can you find out which “process” from the Task Manager is using it

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
ermonyi
post Dec 23 2017, 04:49 PM
Post#6



Posts: 19
Joined: 7-January 06



I´d try to close the process using the PID with this code
CODE
Call Shell("TaskKill /F /PID " & CStr(vPID), vbHide)

but with the chm file open I´m not able to see the name of the process or the PID in the Task Manager.

--------------------
Regards from Sevilla (España)
Go to the top of the page
 
ermonyi
post Dec 24 2017, 03:26 AM
Post#7



Posts: 19
Joined: 7-January 06



Good morning.

I've found 2 cases:
Case 1: I copy manually the file to de Application.CurrentProject.Path: I can check if it exists, and if it's open or not, and I can delete it with supr key >>OK.

Case 2: I invoke the file from the BD (with F1): I can check if it exists, but it appears always open/locked (even if I close it manually) and I can't delete it with supr key because Access has the file open/locked (but it doesn't appear in the Task Manager).

If I open the file manually, the name of the application in the Task Manager is hh.exe

This is the function I use to check if the file is open:
CODE
Public Function fIsFileOpen(strArchivo As String) As Boolean
On Error Resume Next   ' Turn error checking off.
    Dim filenum As Integer, errnum As Integer
    filenum = FreeFile()   ' Get a free file number.
    
    ' Attempt to open the file and lock it.
    Open strArchivo For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.

On Error GoTo 0        ' Turn error checking back on.
    Select Case errnum
        ' No error occurred. File is NOT already open by another user.
        Case 0
            fIsFileOpen = False
        ' Error number for "Permission denied". File is already opened by another user.
        Case 70
            fIsFileOpen = True
        ' Another error occurred.
        Case Else
            Error errnum
    End Select
End Function

So it seems that after invoke from Access, Access has the file lock while running, so I can´t delete it.

--------------------
Regards from Sevilla (España)
Go to the top of the page
 
theDBguy
post Dec 26 2017, 11:17 AM
Post#8


Access Wiki and Forums Moderator
Posts: 71,915
Joined: 19-June 07
From: SunnySandyEggo


Hi,

In this case, you might have to Kill the file outside of Access.

Depending on your intention, I see two possible approaches:

1. Kill the file only when Access opens because it hasn't been used yet, or

2. Kill the file after you quit Access using a batch file or a script.

Just my 2 cents...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
ermonyi
post Dec 27 2017, 11:41 AM
Post#9



Posts: 19
Joined: 7-January 06



Hi.

Yes, you are right like.png

Thanks.

--------------------
Regards from Sevilla (España)
Go to the top of the page
 
theDBguy
post Dec 27 2017, 11:55 AM
Post#10


Access Wiki and Forums Moderator
Posts: 71,915
Joined: 19-June 07
From: SunnySandyEggo


Hi,

You're welcome. Good luck and let us know how it goes...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    19th April 2018 - 07:40 PM