## 2010. július 26., hétfő

### Calculate the size of a record

Problem/Question/Abstract:

How to calculate the size of a record

Here's some code where StrucGrid is a StringGrid holding the table structure in DBD-like single char indentifiers in Col1 and, if applicable, field size in Col2. SpinEdit2 holds the blocksize in byte.

procedure TMainFrm.CalculateRecordSizeClick(Sender: TObject);
var
MaxRecs, RecSize, RecsPerBlock, FreeSpace: Longint;
i: Integer;
begin
RecSize := 0;
with StrucGrid do
begin
for i := 0 to pred(RowCount) do
begin
case Cells[1, i][1] of
'A': RecSize := RecSize + StrToInt(Cells[2, i]);
'D', 'T', 'I', '+': RecSize := RecSize + 4;
'N', '\$', 'Y', '@': RecSize := RecSize + 8;
'M', 'B', 'F', 'O', 'G': RecSize := RecSize + 10 + StrToInt(Cells[2, i]);
'S': RecSize := RecSize + 2;
'L': RecSize := RecSize + 1;
end;
end;
end;
RecsPerBlock := (SpinEdit2.Value - 6) div RecSize;
FreeSpace := (SpinEdit2.Value - 6) - (RecSize * RecsPerBlock);
MaxRecs := 65536 * RecsPerBlock;
ShowMessage('Record Size is: ' + IntToStr(RecSize) + ' bytes' + #13#10
+ 'Records per Block: ' + IntToStr(RecsPerBlock) + #13#10
+ 'Unused Space per Block: ' + IntToStr(FreeSpace) + ' bytes' + #13#10
+ 'Max No of Records in Table: ' + FormatFloat('###############,', MaxRecs));
end;