Board index » delphi » Missing first character using before insert with dbgrid

Missing first character using before insert with dbgrid

The setup:
Delphi 6 Pro, MySQL database
using a dbgrid for data entry, datasource of the grid is a query that
has a mastersource to form a master/detail result.

The problem:
When entering a new record in the master table, the dbgrid is "empty"
So when I enter a value into the first record of the detail grid, I
use the before insert and after instert events to fill in default
data.  However, if I want to enter 140 in the first field, 40 is
displayed along with the default data.  How do I get the 1 to get in
there too.  Once that first record is filled in, then using cursor
down to get the next "new" record, everything is fine (I guess the
cursor down keystroke gets lost, which is no problem.)

<code>

procedure TdmData.qLandBeforeInsert(DataSet: TDataSet);
begin
  With qLand do
  Begin
    {MasterSource is Nil when doing a restore}
    If MasterSource = Nil then exit;
    {New record for this Customer fill in defaults}
    If BOF AND EOF Then
    begin
      Qtr := 'NE';
      Sec := '00';
      Twn := '00';
      Rng := '00';
      Mer := '00';
      exit;
    end;
    Last;
    {Don't want a million blank records is the cursor down key is held
down}
    If FieldByName('ACRES').AsInteger < 1 Then Qtr := 'NO'
    Else {duplicate these fields as most are identical}
    begin
      Qtr := FieldByName('QTR').AsString;
      Sec := FieldByName('SEC').AsString;
      Twn := FieldByName('TWN').AsString;
      Rng := FieldByName('RNG').AsString;
      Mer := FieldByName('MER').AsString;
    end;
  End;
end;

procedure TdmData.qLandAfterInsert(DataSet: TDataSet);
begin
  {MasterSource is Nil when doing a restore}
  If qLand.MasterSource = Nil then exit;
  {Million record escape}
  If Qtr = 'NO' then exit;
  With qLand do
  Begin
    FieldByName('QTR').AsString := Qtr;
    FieldByName('SEC').AsString := Sec;
    FieldByName('TWN').AsString := Twn;
    FieldByName('RNG').AsString := Rng;
    FieldByName('MER').AsString := Mer;
    Post;
  End;
  formHailApp.dbgLand.SelectedField := qLandACRES;
end;

</code>

If this is a stupid way to do this, let me know, I'm open to all
suggestions.

John

 

Re:Missing first character using before insert with dbgrid


Quote
John Kebert wrote:

> So when I enter a value into the first record of the detail grid, I
> use the before insert and after instert events to fill in default
> data.  

Your explanation was not very precise. You did not even tell
if these events were for the Master or Detail tables. Also the
names of both of those TTables had been for a help.

So I still don't know exactly what you are trying to do.

But I would forget all attempts to try fill in new data
to Detail Table in Master Table's Before/After Insert events,
if that's what you mean.

I use the approach that I demand the user to fill in those
Master table fields that are absolutely necessary, before
I allow to fill in any of the Detail table values.
  I use the Detail table's BeforeInsert event to make
this check. Show a user message, and use Abort to cut all
the pre-mature Detail table fillings, if proper Master
table values are missing.

Also I use only the Table's own OnNewRecord event to fill
in any of the Default data values. This rule goes both for
the Master and Detail tables.

Markku Nevalainen

Re:Missing first character using before insert with dbgrid


See In-line reply

Quote
"Markku Nevalainen" <m...@iki.fi> wrote in message

news:3DF662FB.4F2D@iki.fi...

Quote
> Your explanation was not very precise. You did not even tell
> if these events were for the Master or Detail tables. Also the
> names of both of those TTables had been for a help.

It was clear to me<g>.

Okay, the master table is customer and the detail table is land (Here in
Saskatchewan, Canada, a farmer [customer] owns/farms many quarters of land.)
The queries are Zeos queries qCustomer, qLand.

Quote

> So I still don't know exactly what you are trying to do.

> But I would forget all attempts to try fill in new data
> to Detail Table in Master Table's Before/After Insert events,
> if that's what you mean.

No, the "fill in data" is in the detail's Before/After Insert events.

Quote

> I use the approach that I demand the user to fill in those
> Master table fields that are absolutely necessary, before
> I allow to fill in any of the Detail table values.
>   I use the Detail table's BeforeInsert event to make
> this check. Show a user message, and use Abort to cut all
> the pre-mature Detail table fillings, if proper Master
> table values are missing.

Excellent idea - thanks. (Silly users, they should *know* they need to fill
in the Master record first!)

Quote

> Also I use only the Table's own OnNewRecord event to fill
> in any of the Default data values. This rule goes both for
> the Master and Detail tables.

Okay, I'll try using the OnNewRecord event, thanks again.  I'll let you know
how I made out.

Quote

> Markku Nevalainen

John

Re:Missing first character using before insert with dbgrid


It worked great, thanks.

John

Other Threads