How to update a column value?

Discussion of open issues, suggestions and bugs regarding Virtual Data Access Components for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
worker17
Posts: 2
Joined: Thu 27 Aug 2015 14:22

How to update a column value?

Post by worker17 » Thu 27 Aug 2015 14:33

I have just started working with VirtualTable, as it looks very promising.

I defined a table with a fleet ID (string) and some fields I want to keep some counts in (integer).

When I find a condition in the data that requires a tally, I use this delphi code:

... UpdateMWO(Grid.cell[7,i]);

with this routine:

procedure TForm1.UpdateMWO(MyIndex : string);
begin
EMPTable.Locate('vtFleet',MyIndex,[]);
EmpTable.FieldByName('vtMWO').AsInteger := EmpTable.FieldByName('vtMWO').AsInteger + 1;
EMPTable.Post;
end;

but there are no updates to the value of the column. When I later move the virtual table to a grid for display:

EMPTable.First;
for i := 1 to 7 do // there are only 7 fleets
begin
AGWB.Grid.Cells[0,i] := EMPTable.FieldByName('vtFleet').AsString;
AGWB.Grid.Cells[1,i] := EMPTable.FieldByName('vtBSEmp').AsString;
AGWB.Grid.Cells[2,i] := EMPTable.FieldByName('vtBSWOEmp').AsString;
AGWB.Grid.Cells[3,i] := EMPTable.FieldByName('vtBSWO').AsString;
AGWB.Grid.Cells[5,i] := EMPTable.FieldByName('vtMEmp').AsString;
AGWB.Grid.Cells[6,i] := EMPTable.FieldByName('vtMWOEmp').AsString;
AGWB.Grid.Cells[7,i] := EMPTable.FieldByName('vtMWO').AsString;
if EMPTable.FieldByName('vtBSWOEmp').AsInteger <> 0 then
AGWB.Grid.Cells[4,i] := FormatFloat('#####0.000',EMPTable.FieldByName('vtBSWO').AsInteger / EMPTable.FieldByName('vtBSWOEmp').AsInteger)
else
AGWB.Grid.Cells[4,i] := '0';
if EMPTable.FieldByName('vtMWOEmp').AsInteger <> 0 then
AGWB.Grid.Cells[8,i] := FormatFloat('#####0.000',EMPTable.FieldByName('vtMWO').AsInteger / EMPTable.FieldByName('vtMWOEmp').AsInteger)
else
AGWB.Grid.Cells[8,i] := '0';
EMPTable.Next;
end;

All the resulting values are the ones that were set when creating the data originally, as with this row:

EmpTable.Append;
EmpTable.FieldByName('vtFleet').AsString := '15';
EmpTable.FieldByName('vtBSEmp').AsInteger := CountTotal('15','BODY');
EmpTable.FieldByName('vtBSWOEmp').AsInteger := 0;
EmpTable.FieldByName('vtBSWO').AsInteger := 0;
EmpTable.FieldByName('vtMEmp').AsInteger := CountTotal('15','MAINT');
EmpTable.FieldByName('vtMWOEmp').AsInteger := 0;
EmpTable.FieldByName('vtMWO').AsInteger := 0;
EmpTable.Post;


CountTotal is a routine that returns an integer value from a SQL count(*) query.

How should a field be updated? I am at a loss at this time.

Thanks for any help.

worker17
Posts: 2
Joined: Thu 27 Aug 2015 14:22

Re: How to update a column value?

Post by worker17 » Thu 27 Aug 2015 16:12

The problem was that the table was not in Edit mode. This is the corrected routine.

procedure TForm1.UpdateMWO(MyIndex : string);
begin
EMPTable.Locate('vtFleet',MyIndex,[]);
EMPTable.Edit;
EmpTable.FieldByName('vtMWO').AsInteger := EmpTable.FieldByName('vtMWO').AsInteger + 1;
EMPTable.Post;
end;

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: How to update a column value?

Post by AlexP » Fri 28 Aug 2015 07:47

Hello,

To edit an entry before assigning a value to a field, the Edit methout must be called from VT. In addition, if you use the locate method, you need to check the return value, if the record is not found the method returns False.

Post Reply