Link to home
Start Free TrialLog in
Avatar of supertedusa
supertedusaFlag for United States of America

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
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of supertedusa

ASKER

Thanks.  I think I can make that work OK.

MT
If you need help with it, let me know.  Thanks for the points.
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
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\test.mdb", tmrListen)

End Sub

Private Sub Listener_FileChanged(Filename 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.
Well, I had just modified your orignal link to use a timer instead, but this is much cleaner.

Thanks, Paul!

MT
You're welcome.  :)