Board index » delphi » OnGetText/OnSetText for TTable

OnGetText/OnSetText for TTable

I have two TTable components (involved in his question):

    Participant(..., ZipID, ...)
    Zip(ID, City, State, Zip)

I use the OnGetText event of Participant.ZipID to return Zip.Zip to the
dbEdit control, rather than show the ID (which would be meaningless to the
user), and OnSetText to set the new Participant.ZipID when the user inputs a
new Zip Code.

In addition, I have two Calculated Fields in Participant:

    City       City for ZipID
    State    State for ZipID

I use OnCalcFields to set these values based off of ZipID.

Problem:

When I enter a new Zip Code, OnSetText is called and looks to work correctly
(I know this only because I traced it).  However, all three fields (City,
State, ZipID) are blank!  Is there something special I need to do in order
to get these values re-displayed?  I would _really_ appreciate any
suggestions regarding this!

TIA,

Eddie

 

Re:OnGetText/OnSetText for TTable


Quote
Eddie Bush wrote:

> When I enter a new Zip Code, OnSetText is called and looks to work correctly
> (I know this only because I traced it).  However, all three fields (City,
> State, ZipID) are blank!  

You better show some code how you are trying to do it. I regularly use
both OnGet and OnSet events succesfully.

Markku Nevalainen

Re:OnGetText/OnSetText for TTable


Great -- they're at the end!

I'd really appreciate it!

TIA -- Eddie

Quote
Markku Nevalainen <m...@iki.fi> wrote in message news:3886C15D.73FB@iki.fi...
> Eddie Bush wrote:

> > When I enter a new Zip Code, OnSetText is called and looks to work
correctly
> > (I know this only because I traced it).  However, all three fields
(City,
> > State, ZipID) are blank!

> You better show some code how you are trying to do it. I regularly use
> both OnGet and OnSet events succesfully.

> Markku Nevalainen

procedure TParticipantDataModule.tblParticipantZipIDGetText(Sender: TField;
  var Text: String; DisplayText: Boolean);
begin
  tblZip.Open;
  tblZip.FindKey([ Sender.Value ]);
  if tblZipID.AsVariant = Sender.Value then
    Text := tblZipZip.AsString
  else
    Text := '';
  tblZip.Close;
  DisplayText := True;
end;

procedure TParticipantDataModule.tblParticipantZipIDSetText(Sender: TField;
  const Text: String);
begin
  tblZip.IndexName := 'ByZip';
  tblZip.Open;
  tblZip.FindKey([ Text ]);
  if tblZipZip.AsString = Text then
    Sender.Value := tblZipID.AsInteger
  else
    Sender.Value := null;
  tblZip.Close;
end;

Re:OnGetText/OnSetText for TTable


Upon further investigation, I believe that the reason for things not working
as I expected was because the DataSource.AutoEdit was True.  Upon changing
this just now, it seems to work as one would expect.  GetText must only be
called if the dataset is in the Browse state.  Can anyone confirm/dispell
this?

Thanks!

Eddie

Quote
Markku Nevalainen <m...@iki.fi> wrote in message news:38878FF0.7EEA@iki.fi...
> Eddie Bush wrote:

> You said your SetText event does not seem to get the DB field
> updated.

> > procedure TParticipantDataModule.tblParticipantZipIDSetText(Sender:
TField;
> >   const Text: String);
> > begin
> >   tblZip.IndexName := 'ByZip';
> >   tblZip.Open;
> >   tblZip.FindKey([ Text ]);
> >   if tblZipZip.AsString = Text then
> >     Sender.Value := tblZipID.AsInteger
> >   else
> >     Sender.Value := null;
> >   tblZip.Close;
> > end;

> I maybe would try it this way:

> procedure TParticipantDataModule.tblParticipantZipIDSetText(Sender:
TField;
>   const Text: String);
> begin
>   tblZip.IndexName := 'ByZip';
>   tblZip.Open;
>   tblZip.FindKey([ Text ]);
>   if tblZipZip.AsString = Text then
>     (Sender as TIntegerField).Value := tblZipID.AsInteger  {Assuming
Sender Field}
>   else    {is of type TIntegerfield}
>     (Sender as TIntegerField).Clear;
>   tblZip.Close;
> end;

> I did not test the code above. But that's the way how I have done
> it, using "Sender as", in GetText/SetText events.

> Markku Nevalainen

Re:OnGetText/OnSetText for TTable


Quote
Eddie Bush wrote:

You said your SetText event does not seem to get the DB field
updated.

Quote
> procedure TParticipantDataModule.tblParticipantZipIDSetText(Sender: TField;
>   const Text: String);
> begin
>   tblZip.IndexName := 'ByZip';
>   tblZip.Open;
>   tblZip.FindKey([ Text ]);
>   if tblZipZip.AsString = Text then
>     Sender.Value := tblZipID.AsInteger
>   else
>     Sender.Value := null;
>   tblZip.Close;
> end;

I maybe would try it this way:

procedure TParticipantDataModule.tblParticipantZipIDSetText(Sender: TField;
  const Text: String);
begin
  tblZip.IndexName := 'ByZip';
  tblZip.Open;
  tblZip.FindKey([ Text ]);
  if tblZipZip.AsString = Text then
    (Sender as TIntegerField).Value := tblZipID.AsInteger  {Assuming Sender Field}
  else                                                     {is of type TIntegerfield}
    (Sender as TIntegerField).Clear;
  tblZip.Close;
end;

I did not test the code above. But that's the way how I have done
it, using "Sender as", in GetText/SetText events.

Markku Nevalainen

Re:OnGetText/OnSetText for TTable


Quote
Eddie Bush wrote:

> Upon further investigation, I believe that the reason for things not working
> as I expected was because the DataSource.AutoEdit was True.  Upon changing
> this just now, it seems to work as one would expect.  GetText must only be
> called if the dataset is in the Browse state.  Can anyone confirm/dispell
> this?

Most of my Datasources always have AutoEdit=True. And I don't know what you
exactly mean by calling that event. I never call it myself, in code written
by myself.

The TTable and Fields call those events automatic, when the Field is getting
or setting data. Of course the Table must be in Edit or Insert mode when
the Field is getting data, but you do never have to take care of it.

Markku Nevalainen

Other Threads