Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding records through DBGrid with SetText and GetText 1

Status
Not open for further replies.

lespaul

Programmer
Joined
Feb 4, 2002
Messages
7,083
Location
US
I'm trying to use the DBGrid to show and add records to my table. In my table I store the information: (the data is in an AS400/iSeries DB2 database. When the court first started using this back in the day, there wasn't (or they didn't use) the date/time field types. The date is a string and the times are integers. I have a routines that convert the string to a date format and the times from integers to time formats)

JURNUM SERVDAT TIMETYPE TIMEIN TIMEOUT
12345 20040601 ORIENT 830 1230
12345 20040602 DLYPNL 930 1100
12345 20040603 DLYPNL 1400 0

(the 0 in time out indicates that there is no time out for that day yet)

My query has a calculated field to determine the amount of time worked. If there is no time out for the day it enters ' - ' for the time out and '??' for the TotalTime. If there is a time out for the day it shows the time.

Here's the calculated field code:

Code:
procedure TfrmJurorInformation.qryJurorHoursCalcFields(DataSet: TDataSet);
begin
  if qryJurorHours.FieldByName('TIMEOUT').AsInteger <> 0 then
   qryJurorHours.FieldByName('TotalTime').Value := FloatToStr((MinuteSpan(IntToTime(qryJurorHours.FieldByName('TimeIn').AsInteger), IntToTime(qryJurorHours.FIeldByName('TimeOut').AsInteger)))/60)
  else
    qryJurorHours.FieldByName('TotalTime').Value := '??';
end;

In my DBGrid I have GetText functions that convert the date and time:

Code:
procedure TfrmJurorInformation.qryJurorHoursSERVDATGetText(Sender: TField;
  var Text: String; DisplayText: Boolean);
begin
  if not VarIsNull(sender.Value) then
    Text := convertdate(Sender.Value);
end;

procedure TfrmJurorInformation.qryJurorHoursTIMEINGetText(Sender: TField;
  var Text: String; DisplayText: Boolean);
begin
  if not VarIsNull(sender.Value) then
    if sender.Value <> 0 then
      Text := FormatDateTime('h:mm AM/PM', IntToTime(StrToInt(Sender.Value)))
    else
      Text := '-';
end;

So now in my DBGrid I have:
Code:
06/01/2004   ORIENT       8:30 am         12:30 am    4.0
06/02/2004   DLYPNL       9:30 am         11:00 am    1.5
06/03/2004   DLYPNL       2:00 pm            -         ??

Works marvelously!

Here's the problem. When I arrow down to a new record, even if I don't enter anything, the record is posted to the database with the JURNUM, a blank SERVDAT and 0 for the times. How do I prevent the new record from being posted if it doesn't have all the information in it? When I arrow down I get:

Code:
06/01/2004   ORIENT       8:30 am         12:30 am    4.0
06/02/2004   DLYPNL       9:30 am         11:00 am    1.5
06/03/2004   DLYPNL       2:00 pm            -         ??
//                          -                -         ??

and the last entry gets posted to the database! How can I stop it?

Also, how can I default the new record's SERVDAT field to today's date?



Leslie
 
Leslie,

What do you mean when you say "How can I stop it" ?

If you mean "how can I stop the last entry begin posted to the database" you should write a BeforePost event handler. This could look something like:
Code:
procedure TfrmJurorInformation.qryJurorBeforePost(DataSet: TDataSet);
begin
  if qryJurorHours.FieldByName('TIMEOUT').AsInteger = 0 then
    Abort;
end;
Obviously your code will be a bit more complicated if you need to check that all your fields are present before posting.

You can also use the BeforePost event handler to default values.
Code:
procedure TfrmJurorInformation.qryJurorBeforePost(DataSet: TDataSet);
begin
  if qryJurorHours.FieldByName('SERVDAT').IsNull then
     qryJurorHours.FieldByName('SERVDAT').AsDateTime := Now;
  if qryJurorHours.FieldByName('TIMEOUT').AsInteger = 0 then
    Abort;
end;
I haven't tested this code so beware of typos!



Andrew
Hampshire, UK
 
Thank you! Thank you!

I'll give this a try today!

les
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top