Solved

Setting Calculated Field Value at runtime ...

Posted on 2007-11-17
4
617 Views
Last Modified: 2013-11-23
Hi Experts,

I have a screen that is a batch processing screen.

For example, i recall an invoice then i go to process it. On the process screen, i have a grid that has lists all the invoices for a particular client. Currently i have a checkbox which is a calculated field and what i want to do is allow the user to select the invoice they want to process (The user may recieve an check to pay multiple invoices).

Here is the scenario, when the screen opens, what i want it to do is automatically tick the invoice that i recalled, so the user can select other invoice afterwards. Then i can run a while loop to see what records have been selected and just process them.

Thank you in advance :)


Trevor
0
Comment
Question by:trevsoft
4 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
Comment Utility
well here's 1 simple way to do it.
keeping a track of the keyfields that you ahve "checked"
in this example I am making teh grid read only, but if you presss "SPACE" it will "check" that row.
The list of checked records is kept in a stringlist.
When you click button1 it does a dummy "process" and shows the customer list in the memo box, then clears the lsit.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Db, Grids, DBGrids, DBTables, Mask, DBCtrls, DBCGrids;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Table1CustNo: TFloatField;
    Table1Company: TStringField;
    Table1Addr1: TStringField;
    Table1Addr2: TStringField;
    Table1City: TStringField;
    Table1State: TStringField;
    Table1Zip: TStringField;
    Table1Country: TStringField;
    Table1Phone: TStringField;
    Table1FAX: TStringField;
    Table1TaxRate: TFloatField;
    Table1Contact: TStringField;
    Table1LastInvoiceDate: TDateTimeField;
    Table1calcfield_Check: TBooleanField;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Table1CalcFields(DataSet: TDataSet);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    function CurrentRecordKeyFields: string;
  public
    { Public declarations }
    checkedlist: TStringList;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  checkedlist := TStringList.Create;
  table1.open;
  // add any records you want automatically ticked here...eg.  checkedlist.add('CN 1221');
  checkedlist.add('1231');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  checkedlist.clear;
  checkedlist.free;
end;

procedure TForm1.Table1CalcFields(DataSet: TDataSet);
var
  s: string;
begin
  s := CurrentRecordKeyFields;
  Table1calcfield_Check.AsBoolean := (checkedlist.IndexOf(s) <> -1);
end;

procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
var
  i: integer;
  s: string;
begin
  if key = ' ' then
  begin
    s := CurrentRecordKeyFields;
    if (Table1calcfield_Check.AsBoolean) then
    begin // about to uncheck it
      i := checkedlist.IndexOf(s);
      if i <> -1 then checkedlist.Delete(i);
    end
    else
    begin // about to check it
      if checkedlist.IndexOf(s) = -1 then
        checkedlist.add(s);
    end;
    table1.refresh;
  end;
end;

function TForm1.CurrentRecordKeyFields: string;
begin
  result := Table1CustNo.AsString;
{ if you have more than 1 keyfield add it here, separate all fields eg. with #28
  result := Table1CustNo.AsString + #28 + Table1company.AsString; }
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.lines.Clear;
  table1.open;
  table1.first;
  while not table1.eof do
  begin
    if checkedlist.IndexOf(CurrentRecordKeyFields) <> -1 then
      memo1.lines.add(Table1CustNo.AsString);
    table1.next;
  end;
  checkedlist.Clear;
  table1.First;
end;

end.


******** FORM FOLLOWS *********

object Form1: TForm1
  Left = 249
  Top = 107
  Width = 824
  Height = 410
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 16
    Top = 24
    Width = 797
    Height = 109
    DataSource = DataSource1
    Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect, dgAlwaysShowSelection, dgConfirmDelete, dgCancelOnExit]
    ReadOnly = True
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    OnKeyPress = DBGrid1KeyPress
  end
  object Memo1: TMemo
    Left = 16
    Top = 144
    Width = 405
    Height = 157
    Lines.Strings = (
      'Memo1')
    TabOrder = 1
  end
  object Button1: TButton
    Left = 16
    Top = 308
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 2
    OnClick = Button1Click
  end
  object Table1: TTable
    OnCalcFields = Table1CalcFields
    DatabaseName = 'DBDEMOS'
    TableName = 'customer.db'
    Left = 144
    Top = 108
    object Table1calcfield_Check: TBooleanField
      FieldKind = fkCalculated
      FieldName = 'calcfield_Check'
      Calculated = True
    end
    object Table1CustNo: TFloatField
      Alignment = taLeftJustify
      CustomConstraint = 'CustNo IS NOT NULL'
      ConstraintErrorMessage = 'CustNo cannot be blank'
      FieldName = 'CustNo'
      DisplayFormat = 'CN 0000'
      MaxValue = 9999
      MinValue = 1000
    end
    object Table1Company: TStringField
      CustomConstraint = 'X IS NOT NULL'
      ConstraintErrorMessage = 'Company Field has to have a value'
      FieldName = 'Company'
      Size = 30
    end
    object Table1Addr1: TStringField
      FieldName = 'Addr1'
      Size = 30
    end
    object Table1Addr2: TStringField
      FieldName = 'Addr2'
      Size = 30
    end
    object Table1City: TStringField
      FieldName = 'City'
      Size = 15
    end
    object Table1State: TStringField
      FieldName = 'State'
    end
    object Table1Zip: TStringField
      FieldName = 'Zip'
      Size = 10
    end
    object Table1Country: TStringField
      FieldName = 'Country'
    end
    object Table1Phone: TStringField
      FieldName = 'Phone'
      Size = 15
    end
    object Table1FAX: TStringField
      FieldName = 'FAX'
      Size = 15
    end
    object Table1TaxRate: TFloatField
      FieldName = 'TaxRate'
      DisplayFormat = '0.00%'
      MaxValue = 100
    end
    object Table1Contact: TStringField
      FieldName = 'Contact'
    end
    object Table1LastInvoiceDate: TDateTimeField
      FieldName = 'LastInvoiceDate'
    end
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 148
    Top = 168
  end
end
0
 
LVL 4

Expert Comment

by:kenpem
Comment Utility
Another way would be to filter the dataset so that only unpaid invoices are shown. Don't show the calculated field in the view, have an ordinary check-box. Then in your post-process, simply update the records the user has checked.
0
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Forced accept.

Computer101
EE Admin
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now