Refresh database automatically

Posted on 2006-03-31
Last Modified: 2010-04-05

I would like to update (refresh) a MS-SQL table let's say every minute. So any new records or changes are presented to me.

Of course refreshing should only be executed when the table is in dsBrowse mode.
Refreshing has only to occur when a certain form is opened.
So user opens the form containing the grid of the MS-SQL table.

Can anybody give me an example on how to implement this? Please keep it as simple as possible.

Question by:Delphiwizard
    LVL 15

    Expert Comment

    Surely the only way would be to close and reopen the query to the table to specifically call a refresh or am I wrong?
    LVL 6

    Expert Comment

    Hello Sir,

       To refresh the only way is to Close and Open the Query as mikelittlewood says. If this should happen when you open the form you could do it on FormActivate, but this will not be called when you move from one application to another application, but calls when you move from one form to another form, if you wish to do this refreshing when you move from one application to another application, then here is the code to do it.

    procedure AppOnActivate(Sender: TObject);
    //declare the above procedure definition in the private section of  Form1.

    procedure TForm1.FormCreate(Sender: TObject);
        Application.OnActivate := AppOnActivate;

    procedure TForm1.AppOnActivate(Sender: TObject);

    with regards,

    Author Comment

    The problem isn't so much refreshing the table, but how to do it automatically.
    Maybe a thread can be used or a timer, but I don't know how to implement it in such a way that it won't interfere with changes made by the enduser.
    There is no other application that needs access to the database.

    Author Comment

    By the way I use a TMySQLTable that has property Refresh.
    LVL 11

    Expert Comment

    Another approach is to use a timer that runs a stored procedure that returns some sort of checksum on the table, if that value has changed then refresh the data else don't bother,

    Author Comment

    pcsentinel: Could you give an example for that?
    LVL 11

    Accepted Solution

    say your table has

    ID > int AutoID
    Name > clientname
    Address > address
    LastUpdate > int > field that is incremented everytime the record is edited

    right a stored proc that

    sums the ID+LastUpdate field for all records

    sorry dont know MySQL so not sure of Syntax, but for SQL Server would be something like

    @Checksum int OUTPUT
      SET @Checksum=(SELECT SUM(ID + LastUpdate) FROM MyTable)

    when they first open the table run the query to get your staring checksum

    then on a timer say every 5 secs, run the GetCheckSum procedure and if its different then you know you need to refresh the display, check that the user is in dsBrowse mode, if not then you dont need to worry because they will refresh the data after the edit.

    Author Comment

    That is very nice suggestion.
    I've tested it and it works just fine.
    Thank you.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now