G
Guest
I would like to modify this program from an MVP site to work with Access 2003.
Option Compare Database
Private Declare Function CreateFile& Lib "kernel32" Alias "CreateFileA"
(ByVal _
lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode
As Long, _
lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Public Function FileExists(strFileName As String, ChkType As Byte) As Long
'--------------------------------------------------------------------------- _
Purpose: Checks for file existence and read/write capability. _
Returns: File handle number (>0) if file exists and is useable based on the
ChkType _
a negative number if there is an error (e.g. the file is not
found or is locked.) _
ChkType: 1=Check for existence, 2= Check for file lock, 3 = Check if file is
share writeable. _
----------------------------------------------------------------------------
Dim lngFileHandle As Long
Dim lngLastError As Long
Const FILE_SHARE_READ = &H1: Const FILE_SHARE_WRITE = &H2
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const OPEN_EXISTING = 3&
'Possible return Error values (multiplied by -1)
Const INVALID_FILE_HANDLE = -1
Const ERROR_FILE_NOT_FOUND = 2: Const ERROR_PATH_NOT_FOUND = 3
Const ERROR_ACCESS_DENIED = 5: Const ERROR_INVALID_DRIVE = 15
Const ERROR_DEVICE_NOT_READY = 21: Const ERROR_SHARING_VIOLATION = 32
Const ERROR_LOCK_VIOLATION = 33: Const ERROR_NETWORK_UNREACHABLE = 1231
Select Case ChkType
Case 1
'Simple check for existence, no read-write desired
lngFileHandle = CreateFile(strFileName, 0, 0, ByVal 0&, OPEN_EXISTING, _
0, ByVal 0&)
Case 2
'Check whether file is locked if write access is desired
lngFileHandle = CreateFile(strFileName, GENERIC_READ Or GENERIC_WRITE, _
0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
Case 3
'Read-write desired, share allowed
lngFileHandle = CreateFile(strFileName, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0,
ByVal 0&)
End Select
CloseHandle (lngFileHandle)
lngLastError = Err.LastDllError
If lngLastError <> 0 Then
FileExists = lngLastError * -1
Else
FileExists = lngFileHandle
End If
End Function
thanks,
Option Compare Database
Private Declare Function CreateFile& Lib "kernel32" Alias "CreateFileA"
(ByVal _
lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode
As Long, _
lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Public Function FileExists(strFileName As String, ChkType As Byte) As Long
'--------------------------------------------------------------------------- _
Purpose: Checks for file existence and read/write capability. _
Returns: File handle number (>0) if file exists and is useable based on the
ChkType _
a negative number if there is an error (e.g. the file is not
found or is locked.) _
ChkType: 1=Check for existence, 2= Check for file lock, 3 = Check if file is
share writeable. _
----------------------------------------------------------------------------
Dim lngFileHandle As Long
Dim lngLastError As Long
Const FILE_SHARE_READ = &H1: Const FILE_SHARE_WRITE = &H2
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const OPEN_EXISTING = 3&
'Possible return Error values (multiplied by -1)
Const INVALID_FILE_HANDLE = -1
Const ERROR_FILE_NOT_FOUND = 2: Const ERROR_PATH_NOT_FOUND = 3
Const ERROR_ACCESS_DENIED = 5: Const ERROR_INVALID_DRIVE = 15
Const ERROR_DEVICE_NOT_READY = 21: Const ERROR_SHARING_VIOLATION = 32
Const ERROR_LOCK_VIOLATION = 33: Const ERROR_NETWORK_UNREACHABLE = 1231
Select Case ChkType
Case 1
'Simple check for existence, no read-write desired
lngFileHandle = CreateFile(strFileName, 0, 0, ByVal 0&, OPEN_EXISTING, _
0, ByVal 0&)
Case 2
'Check whether file is locked if write access is desired
lngFileHandle = CreateFile(strFileName, GENERIC_READ Or GENERIC_WRITE, _
0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
Case 3
'Read-write desired, share allowed
lngFileHandle = CreateFile(strFileName, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0,
ByVal 0&)
End Select
CloseHandle (lngFileHandle)
lngLastError = Err.LastDllError
If lngLastError <> 0 Then
FileExists = lngLastError * -1
Else
FileExists = lngFileHandle
End If
End Function
thanks,