• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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
0
supertedusa
Asked:
supertedusa
  • 4
  • 3
1 Solution
 
PaulHewsCommented:
See the following:
Creating a Watched Folder with FindChangeNotification
http://vbnet.mvps.org/code/fileapi/watchedfolder.htm
0
 
supertedusaAuthor Commented:
Thanks.  I think I can make that work OK.

MT
0
 
PaulHewsCommented:
If you need help with it, let me know.  Thanks for the points.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
supertedusaAuthor Commented:
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
0
 
PaulHewsCommented:
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.
0
 
supertedusaAuthor Commented:
Well, I had just modified your orignal link to use a timer instead, but this is much cleaner.

Thanks, Paul!

MT
0
 
PaulHewsCommented:
You're welcome.  :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now