Re: using Tab in DBGrid - automatic post

Posted by webmaster Guido on June 16, 2003
In Reply to: using Tab in DBGrid - automatic post posted by Stefaan Breugelmans on June 14, 2003:

: I have a DBGrid connected to a paradox TTable, and I have noticed that when the Dataset is in Insert or Edit mode, pressing the Tab key will perform an automatic Post of the Dataset, instead of going to the next column to insert/edit the other information.
: I am creating an appointment book, and the first column is the hour, second column the name and the third column what the appointment is for.
: I enter the hour of the appointment and press Tab to go to the Name Column, but instead it automatically Post already the hour, without a name or reason.
: I have noticed that there is a dgTab setting but when this is set to Fault, it just ignores when I press Tab.
: Is there anyway around it ?

With the default settings, an automatic "Post" is only caused by pressing TAB if you are in the last column. I'll leave it up to you to find out what causes the other Posts, but here are some general ideas:

* As you noted, you should leave the DBGrid's option "dgTabs" set to True, otherwise the user can't use the Tab and Shift+Tab keys for navigation in the grid.

* But it's not only the TAB key that should worry you, because automatic posting happens whenever you leave the current row of the DBGrid (if you go to another record)! That means that all these actions post the record:

- there is a mouse click in another row;
- you press ANY cursor key that changes the row (UP, DOWN,...);
- when you press TAB in the last column
- when you press Shift-TAB in the first column;
- and so on...

Solution: check the contents of all the fields right before a record is posted, and block the posting if necessary. The best place to do this is in the BeforePost event of the table, like this:

procedure TFormMain.Table1BeforePost(DataSet: TDataSet);
  if (Table1.FieldByName('Hour').AsString = '') or
     (Table1.FieldByName('Name').AsString = '') or
     (Table1.FieldByName('What').AsString = '') then begin
    ShowMessage('Please enter something in ALL the fields');
    Abort; // no Post if any of the fields is empty

Find related articles

Search for:  DBGrid   Dataset  Table

Delphi Forum :: Tutorials :: Source code :: Tips