Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting Calculated Field Value at runtime ...

Posted on 2007-11-17
4
Medium Priority
?
628 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 2000 total points
ID: 20308883
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
ID: 20318811
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
ID: 21216624
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

885 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