supertedusa
asked on
Possible to detect that an mdb file has been altered outside my program?
Hello,
I have an app that's using an Access database that exchanges info from a Palm through AppForge's Crossfire product.
Is there a way to have a listener or something similar in my client app that can detect when the Palm sync has altered the Access db?
Thanks,
MT
I have an app that's using an Access database that exchanges info from a Palm through AppForge's Crossfire product.
Is there a way to have a listener or something similar in my client app that can detect when the Palm sync has altered the Access db?
Thanks,
MT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you need help with it, let me know. Thanks for the points.
ASKER
Paul, this works, but the WaitForSingleObject call obviously ties up the program. Suggestions for allowing this to run in the background while the program moves on?
Thanks,
MT
Thanks,
MT
Here's a much simpler solution to listen for changes on a single file. Add a class to your project named clsListener. Add the following code:
Option Explicit
Private mdtFileModTime As Date
Private mlngFileSize As Long
Private mbListening As Boolean
Private mstrFilename As String
Public Event FileChanged(Filename As String)
Private WithEvents tmrListener As Timer
Public Sub StartListening(Filename As String, tmr As Timer, Optional Interval As Integer = 5000)
If Len(Dir(Filename)) > 0 Then
Set tmrListener = tmr
mbListening = True
Call SetFileProps(Filename)
tmrListener.Interval = Interval
tmrListener.Enabled = True
End If
End Sub
Public Sub StopListening()
mbListening = False
mstrFilename = ""
tmrListener.Enabled = False
tmrListener.Interval = 0
End Sub
Private Sub tmrListener_Timer()
If mbListening Then
If mlngFileSize <> FileLen(mstrFilename) Then
Call SetFileProps(mstrFilename)
RaiseEvent FileChanged(mstrFilename)
Exit Sub
End If
If mdtFileModTime <> FileDateTime(mstrFilename) Then
Call SetFileProps(mstrFilename)
RaiseEvent FileChanged(mstrFilename)
Exit Sub
End If
End If
End Sub
Private Sub SetFileProps(Filename As String)
mstrFilename = Filename
mlngFileSize = FileLen(Filename)
mdtFileModTime = FileDateTime(Filename)
End Sub
Now on your form add a timer and use this code to create and start the listener:
Option Explicit
Private WithEvents Listener As clsListener
Private Sub Form_Load()
Set Listener = New clsListener
Call Listener.StartListening("C :\temp\tes t.mdb", tmrListen)
End Sub
Private Sub Listener_FileChanged(Filen ame As String)
MsgBox Filename & " changed."
End Sub
You can create more than one Listener, but you have to use a separate timer for each. If not, you will have collisions.
Option Explicit
Private mdtFileModTime As Date
Private mlngFileSize As Long
Private mbListening As Boolean
Private mstrFilename As String
Public Event FileChanged(Filename As String)
Private WithEvents tmrListener As Timer
Public Sub StartListening(Filename As String, tmr As Timer, Optional Interval As Integer = 5000)
If Len(Dir(Filename)) > 0 Then
Set tmrListener = tmr
mbListening = True
Call SetFileProps(Filename)
tmrListener.Interval = Interval
tmrListener.Enabled = True
End If
End Sub
Public Sub StopListening()
mbListening = False
mstrFilename = ""
tmrListener.Enabled = False
tmrListener.Interval = 0
End Sub
Private Sub tmrListener_Timer()
If mbListening Then
If mlngFileSize <> FileLen(mstrFilename) Then
Call SetFileProps(mstrFilename)
RaiseEvent FileChanged(mstrFilename)
Exit Sub
End If
If mdtFileModTime <> FileDateTime(mstrFilename)
Call SetFileProps(mstrFilename)
RaiseEvent FileChanged(mstrFilename)
Exit Sub
End If
End If
End Sub
Private Sub SetFileProps(Filename As String)
mstrFilename = Filename
mlngFileSize = FileLen(Filename)
mdtFileModTime = FileDateTime(Filename)
End Sub
Now on your form add a timer and use this code to create and start the listener:
Option Explicit
Private WithEvents Listener As clsListener
Private Sub Form_Load()
Set Listener = New clsListener
Call Listener.StartListening("C
End Sub
Private Sub Listener_FileChanged(Filen
MsgBox Filename & " changed."
End Sub
You can create more than one Listener, but you have to use a separate timer for each. If not, you will have collisions.
ASKER
Well, I had just modified your orignal link to use a timer instead, but this is much cleaner.
Thanks, Paul!
MT
Thanks, Paul!
MT
You're welcome. :)
ASKER
MT