Re: Getting current date and time
Posted by webmaster Guido on November 15, 2002 at 03:31:50:
In Reply to: Getting current date and time posted by Lesley Gousmett + P12256 on November 12, 2002 at 21:26:31:
: I have a TDBText component on my form which is a persistent database field and is named "dbStartTime". The Datasource is called "dsHire" and the datafield is "WhenStart". I want "dbStartTime" to display the current date and time, when the user clicks a button on the form called "New Hire". Can you tell me how I can do this?
You probably mean: a TDBText component which *shows the value of* a persistent database field and *the TDBText component* is named "dbStartTime". You don't mention the name of the persistent field, so let's assume that it is named "tableHireWHENSTART", the default name that Delphi creates if you use a TTable component with the name "tableHire".
Note that the persistent field does not display anything, because a field is a non-visual object. The displaying is done by the TDBText (or you might show the date in a TDBGrid, in a TDBEdit or other component). To clearify things, let's look at an example.
1. There is a physical table, say "Hire.dbf" (or a Paradox table "Hire.db" or whatever kind of table). One of its fields is a date-field, named "WhenStart".
2. Components on the Delphi form:
3. You created persistent fields for the table, and Delphi gave the name "tableHireWHENSTART" to the date-field.
procedure TForm1.btnNewHireClick(Sender: TObject); begin tableHire.Append; tableHireWHENSTART.Value := Date; end;
The first line puts the TTable component in "Append" mode.
After a click on btnNew you see today's date in the TDBText; now it's in the persistent field, but not yet in the table. In order to save it in the table, you must "post" the record, after having filled the other fields (such as LastName, FirstName, and so on...). Posting a record can be done in various ways: in code by giving the command tableHire.Post, or by clicking the post-button of a TTableNavigator that is connected to the TDataSource; note that also moving to another row in a TDBGrid posts the record that is being appended or edited.