Solved

Passing a parameter to new opened form.

Posted on 2004-04-21
13
562 Views
Last Modified: 2010-04-05
Hi folks,

Here is my proplem. There is a main form (frmMain). There is a text box (edtOrderNumber). When you enter whole order number (99000123) or just a part of it (123) it searchs the mysql database and returns the value. if it finds only one result it fills all fields like edtBillingName, edtBillingSurname etc. The thing I want to do is if it finds more than one result it will open a new form (frmFoundOrders) with a ListView on the form (lvFoundOrders) and show all found results on this new form with OrderNumber and CustomerSurname values. When user clicks on one of these results this form (frmFoundOrders) closes and the ordernumber value passes to main form (frmMain) and fills the fields (edtBillingName, edtBillingSurname etc. ).

I could manage to do if the result just one.

As you can understand I'm trying a develop the admin page of our ecommerce site on Delphi 7. Order numbers are uniqe.

Thanks in advance for your help.
0
Comment
Question by:bilgehanyildirim
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10884172
So then what you  want to do is Create a New form (formStyle = fsDialog) with a TListView  component - with ViewSTyle property set to vsReport.

Right click on it and set up your columns.

Then add the code to fill in the ListView with your QUery returned set.

var
 Item: TListItem;

 MyQuery.First
 While not MyQuery.EOF do
 begin
  Item:= LIstView.Items.Add;
  Item.Caption:=  MyQuery.FieldByName('MyField1').asString;
  Item.SubItems.Add(MyQuery.FieldByName('MyField2').asString);
  //note add additional subitems for each additional field MyField3.....and so on
 end;

call the form like

 if MyNewFOrm.SHowModal = mrOK then
 begin
  FIllOrder(MyNewForm.ListView1.Selected);
 end;

You can create a button with the modal result = mrOK or create code so when you double click a ListView Item, you send the ModalResult = mrOK

procedure FIllOrder(Item: TListItem)
begin
 MyEdit1.Text:= Item.Caption;
 MyEdit2.Text:= Item.SubItems[0];
 MyEdit3.Text:= Item.SubItems[1];
 .... and so on.....
end;


Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10884179
Oops!

MyQuery.First
 While not MyQuery.EOF do
 begin
  Item:= LIstView.Items.Add;
  Item.Caption:=  MyQuery.FieldByName('MyField1').asString;
  Item.SubItems.Add(MyQuery.FieldByName('MyField2').asString);
  //note add additional subitems for each additional field MyField3.....and so on

  MyQuery.Next;  ///************

 end;


Shane
0
 
LVL 17

Expert Comment

by:mokule
ID: 10884190
Hi

// create frmFoundOrders here or at application start
frmFoundOrders.      // set whatever You want
if frmFoundOrders.ShowModal = mrOK then
  begin
  frmFoundOrders.lvFoundOrders.Selected... // get whatever You want
  end;
// Free frmFoundOrders here or at application ends
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10884204
Another oops!

if MyNewFOrm.SHowModal = mrOK then
 begin
  MainForm.FillOrder(MyNewForm.ListView1.Selected);
 end;

procedure MainFOrm.FillOrder(Item: TListItem)
begin
  MyEdit1.Text:= Item.Caption;
  MyEdit2.Text:= Item.SubItems[0];
  MyEdit3.Text:= Item.SubItems[1];
  .... and so on.....
end;

Shane

0
 
LVL 17

Expert Comment

by:mokule
ID: 10884232

If You've got Query use rather DBGrid on Your frmFoundOrders
0
 

Author Comment

by:bilgehanyildirim
ID: 10884245
Thanks all of you! I was quicker than I expected :)

Shane, could you please show me how can I both close the new form and send the value to mainform?

You said
"You can create a button with the modal result = mrOK or create code so when you double click a ListView Item, you send the ModalResult = mrOK"
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:bilgehanyildirim
ID: 10884249
I'm using different component than the one which comes with D7 and to be hones I hate DBGrid component :)
0
 
LVL 17

Expert Comment

by:mokule
ID: 10884306

You may hate DBGrid but in such a case use other DB aware controls
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10884328
A Button has a ModalResult property which you can set to mrOK, so if you hvae a form style of fsDialog, cllicking this button will pass the ModalResult back to form


or you can do it by code

procedure TForm1.Button1Click(Sender: TObject);
begin
 ModalResult:= mrOK ;
 Close;
end;


so then your main form tests it

 if MyNewFOrm.SHowModal = mrOK then
 begin
  FillOrder(MyNewForm.ListView1.Selected);
 end;

Shane



0
 
LVL 17

Expert Comment

by:mokule
ID: 10884332

It is convenient to have data module. You don't need then to send results back. Results selected in Your frmFoundOrders form will be readable in main form.
0
 

Author Comment

by:bilgehanyildirim
ID: 10884343
can you give any example for usage of datamodule?
0
 
LVL 17

Accepted Solution

by:
mokule earned 125 total points
ID: 10886243
This is a very simple example with datamodule and 2 forms.
On datamodule there are few components but at the moment only two are functioning in this simple application DataSource1 and Table1

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Mask, DBCtrls;

type
  TForm1 = class(TForm)
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2, Unit3;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
  DataModule2.Table1.Active := True;
  if Form3.ShowModal <> mrOk then
    DataModule2.Table1.Active := False;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.Caption := 'Select';
end;

end.
{-----------------------------------}
unit Unit2;

interface

uses
  SysUtils, Classes, ADODB, DB, DBTables;

type
  TDataModule2 = class(TDataModule)
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    Table1: TTable;
    Query1: TQuery;
    DataSource4: TDataSource;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    ADOQuery1: TADOQuery;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DataModule2: TDataModule2;

implementation

{$R *.dfm}

end.
{-----------------------------------}
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, StdCtrls;

type
  TForm3 = class(TForm)
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm3.Button2Click(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  Button1.Caption := 'OK';
  Button2.Caption := 'Cancel';
end;

end.
{-----------------------------------}
{unit1.dfm}

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 544
  Height = 168
  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
  PixelsPerInch = 96
  TextHeight = 13
  object DBEdit1: TDBEdit
    Left = 40
    Top = 48
    Width = 121
    Height = 21
    DataField = 'FIRST_NAME'
    DataSource = DataModule2.DataSource1
    TabOrder = 0
  end
  object DBEdit2: TDBEdit
    Left = 192
    Top = 48
    Width = 121
    Height = 21
    DataField = 'LAST_NAME'
    DataSource = DataModule2.DataSource1
    TabOrder = 1
  end
  object DBEdit3: TDBEdit
    Left = 344
    Top = 48
    Width = 121
    Height = 21
    DataField = 'ADDRESS_1'
    DataSource = DataModule2.DataSource1
    TabOrder = 2
  end
  object Button1: TButton
    Left = 40
    Top = 88
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 3
    OnClick = Button1Click
  end
end
{-----------------------------------}
{unit2.dfm}
object DataModule2: TDataModule2
  OldCreateOrder = False
  Left = 193
  Top = 116
  Height = 345
  Width = 307
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 136
    Top = 40
  end
  object DataSource2: TDataSource
    DataSet = Query1
    Left = 136
    Top = 96
  end
  object DataSource3: TDataSource
    DataSet = ADOTable1
    Left = 136
    Top = 152
  end
  object Table1: TTable
    DatabaseName = 'DBDEMOS'
    TableName = 'clients.dbf'
    Left = 208
    Top = 40
  end
  object Query1: TQuery
    DatabaseName = 'DBDEMOS'
    Left = 208
    Top = 96
  end
  object DataSource4: TDataSource
    DataSet = ADOQuery1
    Left = 136
    Top = 208
  end
  object ADOConnection1: TADOConnection
    Left = 40
    Top = 152
  end
  object ADOTable1: TADOTable
    Connection = ADOConnection1
    Left = 208
    Top = 152
  end
  object ADOQuery1: TADOQuery
    Connection = ADOConnection1
    Parameters = <>
    Left = 208
    Top = 208
  end
end
{-----------------------------------}
{unit3.dfm}
object Form3: TForm3
  Left = 192
  Top = 114
  Width = 544
  Height = 235
  Caption = 'Form3'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 8
    Top = 16
    Width = 505
    Height = 129
    DataSource = DataModule2.DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object Button1: TButton
    Left = 8
    Top = 160
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 120
    Top = 160
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 2
    OnClick = Button2Click
  end
end
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10886400
just to show another way

----- the selectform (generic) ----------------

unit frmSelect_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, db;

type
  TForm2 = class(TForm)
    ListView1: TListView;
    Button1: TButton;  //modalresult set to mrOK
    Button2: TButton;  //modalresult set to mrCancel
  private
    function BuildColumns(ADataSet : TDataSet; ResultFieldName : String) : Integer;
    procedure FillContent(ADataSet : TDataSet);
  public
    class function Select(AOwner : TComponent; ADataSet : TDataSet; ResultFieldName : String) : Integer;
  end;


var
  Form2: TForm2;

implementation

{$R *.dfm}

function TForm2.BuildColumns(ADataSet : TDataSet; ResultFieldName : String) : Integer;
var
  i : integer;
  t : TListColumn;
begin
  result := -1;
  if assigned(ADataset) and (AdataSet.Active) then
  begin
    for i := 0 to ADataSet.FieldCount-1 do
    begin
      t := listview1.Columns.Add;
      t.Caption := ADataSet.Fields[i].FieldName;
      if UpperCase(ADataSet.Fields[i].FieldName) = UpperCase(ResultFieldName) then
        result := i;
    end;
  end
  else raise Exception.Create('Invalid Dataset or Dataset closed');
end;


procedure TForm2.FillContent(ADataSet : TDataSet);
var
  l : TListItem;
  i : integer;
begin
  if assigned(ADataset) and (AdataSet.Active) then
  begin
    ADataSet.First;
    while not ADataSet.eof do
    begin
      l := listview1.Items.Add;
      l.Caption := ADataSet.Fields[0].AsString;
      for i := 1 to ADataSet.FieldCount-1 do
        l.SubItems.Add(ADataSet.Fields[i].AsString);
      ADataset.Next;
    end;
  end
  else raise Exception.Create('Invalid Dataset or Dataset closed');
end;


class function TForm2.Select(AOwner : TComponent; ADataSet : TDataSet; ResultFieldName : String) : Integer;
var
  f : TForm2;
  t : TListColumn;
  i : integer;
  resultFieldIndex : Integer;
begin
  result := -1;
  if (assigned(ADataSet)) and (ADataSet.Active) then
  begin
    f := Create(AOwner);
    try
      resultFieldIndex := f.BuildColumns(ADataset,ResultFieldName);
      f.FillContent(ADataSet);
      if f.ShowModal = mrOK then
      begin
        if (resultFieldIndex = -1) or (not(assigned(f.ListView1.Selected))) then
          //nothing
        else
          if resultFieldIndex = 0 then
            result := strtoint(f.ListView1.Selected.Caption)
          else
            result := strtoint(f.ListView1.Selected.SubItems[resultFieldIndex-1]);
      end;
    finally
      f.Release;
    end;
  end
  else raise Exception.Create('Invalid Dataset or Dataset closed');
end;

end.

------------------------ usage sample ----------

procedure TForm1.Button1Click(Sender: TObject);
begin
  adoquery1.open;
  //form2 must not created
  showmessage(inttostr(TForm2.Select(self,adoquery1,'AID')));
end;


meikl ;-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

18 Experts available now in Live!

Get 1:1 Help Now