?
Solved

Possible to detect that an mdb file has been altered outside my program?

Posted on 2005-03-15
7
Medium Priority
?
186 Views
Last Modified: 2010-05-02
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
Comment
Question by:supertedusa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 750 total points
ID: 13547833
See the following:
Creating a Watched Folder with FindChangeNotification
http://vbnet.mvps.org/code/fileapi/watchedfolder.htm
0
 

Author Comment

by:supertedusa
ID: 13548077
Thanks.  I think I can make that work OK.

MT
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 13548343
If you need help with it, let me know.  Thanks for the points.
0
Technology Partners: 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!

 

Author Comment

by:supertedusa
ID: 13556376
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 13556831
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
 

Author Comment

by:supertedusa
ID: 13557257
Well, I had just modified your orignal link to use a timer instead, but this is much cleaner.

Thanks, Paul!

MT
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 13557316
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question