Solved

Setting Calculated Field Value at runtime ...

Posted on 2007-11-17
4
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

623 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