upgrade a program to Access 2003

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,
 
G

Guest

I don't understand what is the kernel32, the read and write parameters, or
the dllerror. This program does more than I need it to do, it is a bit
complex. Moving up from Excel/VBA to Access/VBA is a stretch for me but I'm
trying to understand it. Thanks for letting me know this should work.
 
G

Guest

What I really need to do is figure out how to see if an event occured first
before I check the directory to see if the file exists. I can then modify
some of the code above to make it simpler but I have to see if the user added
a record in the datasheet . So the event would be afterUpdate but there are
no properties in the table datasheet, so how does it know when the record got
added?
Do I have to create a class module? It still has to know when the event
fired so does that mean the only way to tell if the record got created is to
create a user form? For this case, I don't need a user form.
 
G

George Nicholson

In Access, Tables don't have events (or triggers), so if you need to know if
the user has added a record then you do indeed need a form in this case.

BTW, if FileExists (and its related API calls) is more than you need, look
at the DIR() function.

If Dir(strPathAndFilename) = "" Then
' File does not exist
Else
' File does exist
End If


HTH,
 
T

Tom van Stiphout

On Sat, 27 Oct 2007 15:41:00 -0700, Janis

You seem a bit lost. Perhaps it's time to get professional help.
"Microsoft Solution Provider" in your yellow pages is a good place to
start.

There are no events that fire when a record is added to a table view.
It is highly recommended never to allow users to edit the tables
directly. Take a few minutes to create a form (what you call "user
form"). Heck, the form wizard can do it for you in a few seconds. Then
the events are available and Form_AfterUpdate is the event that fires
after a record has been updated or added. Check out Form_AfterInsert
as well.

The code you quoted in first instance goes in a standard module, not a
class module.

-Tom.
 
R

Rafael

Janis said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top