Lazarus

Programming => Databases => Topic started by: Bert_Plitt on November 13, 2019, 06:56:46 pm

Title: (SOLVED) Get value of a field of a highlighted record
Post by: Bert_Plitt on November 13, 2019, 06:56:46 pm
I'm a SQLite3 newbee working on a test project (attached).  I've got most things working as desired, but can't figure out how to do the following.  If I highlight one of the records (in the grid) and click btnSelect, I want the value of Field 3 to be copied to the static label.  After the copying is done, ALL the records in the table must remain visible in the grid.  Can anyone suggest code (for the OnClick event) that will assign the value of the highlighted record field into the str variable?

Windows 10, Laz 2.0.6, FPC 3.0.4
Title: Re: Get value of a field of a highlighted record
Post by: fmc on November 13, 2019, 07:17:12 pm
Not sure if this is what you're looking for.

Code: Pascal  [Select]
  1. var
  2.     Str: String;
  3. begin
  4.     // Assign value of highlighted record, "Field 3" to Str
  5.     Str:=Query1.Fields[2].AsString; // FIELD 3
  6.  
  7.     txtItem3.Caption:=Str;
  8.     // All Records must remain visible in "NAME" grid.
  9. end;
Title: Re: Get value of a field of a highlighted record
Post by: Bert_Plitt on November 13, 2019, 10:38:23 pm
fmc -- YES!  That's exactly what I wanted.  Works like a charm.  I was off in the weeds trying to use SELECT and/or FieldByName.  Thanks.
Title: Re: Get value of a field of a highlighted record
Post by: fmc on November 14, 2019, 03:26:04 am
You can use FieldByName and get the same result:

Code: Pascal  [Select]
  1. var
  2.     Str: String;
  3. begin
  4.     // Assign value of highlighted record, "Field 3" to Str
  5.     Str:=Query1.FieldByName('Field3').AsString;
  6.  
  7.     txtItem3.Caption:=Str;
  8.     // All Records must remain visible in "NAME" grid.
  9. end;
Title: Re: Get value of a field of a highlighted record
Post by: Bert_Plitt on November 15, 2019, 02:19:51 am
fmc -- Yes, that's how I have it in my actual project.  I tried FieldByName when I started, but for some unknown reason Str always contained an empty string.  Evidently I must have changed something in my code because now FieldByName is working.  Thanks for your help.

BTW - Do you know how to mark messages as "SOLVED"?
Title: Re: Get value of a field of a highlighted record
Post by: GAN on November 15, 2019, 02:32:43 am
Bert_Plitt to mark solved you have to edit your first message.
Use Modify button to do it.