How to retrieve data from Modal forms

I have for example a unit A that call a modal form in Unit B. In Unit B, items are selected using Multiselect in the DBGrid shown in unit B. What I need is to process the items selected in Unit A after the form in unit B is closed. How to go about it?

SA/\/\
"If you have always done it that way, it is probably wrong."
LVL 1
samchanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

millerwCommented:
I normally have my modal forms created when used.  That is why the following TfrmSaveSearch is being created then freed.  The "txtName.Text" is being read of the TfrmSaveSearch.  Just access them by direct calls.  JUST DON'T SET THE FORMCLOSE EVENT TO CAFREE OR THIS WILL TOTALLY MESS YOU UP--ACCESS VIOLATIONS GALORE.  Exit out of the form by setting ModalResult to something like mrOK or mrCancel (the regular buttons do this automatically but Speed buttons don't).  Free up the form after you read everything you need--as you can see below.

With TfrmSaveSearch.Create(Self) do
try
  If ShowModal = mrOK then
  begin
    FSearch.Save(txtName.Text, ssUser);
  end;
finally
  Free;
end;

Scott
0
samchanAuthor Commented:
I did try that. However, those SelectedRows of ItemDBGrid somehow do not have any values after the ShowModal line! But those SelectedRows are still available if I check it in the Modal Unit.

The codes I am trying is as follows:

begin
 with TfrmItemSelection.Create(Self) do
  try
    if ShowModal = mrOk then
    begin
      if ItemDBGrid.SelectedRows.Count>0 then
      begin
        with ItemDBGrid.DataSource.DataSet do
        begin
          for i:=0 to ItemDBGrid.SelectedRows.Count-1 do
          begin
            GotoBookmark(pointer(ItemDBGrid.SelectedRows.Items[i]));
            for j := 0 to FieldCount-1 do
            begin
              if (j>0) then s:=s+', ';
              s:=s+Fields[j].AsString;
            end;
            Listbox1.Items.Add(s);
              s:= '';
          end;
        end;
      end;
    end;
  finally
    Free;
  end;
end;
0
kambizCommented:
You probably close DataSet on Hide event of your form.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

samchanAuthor Commented:
Yup. You're right. I closed the Dataset.
Thank you.
Hmm... now who should I give the point too.
0
millerwCommented:
Go for it kambiz
0
samchanAuthor Commented:
Okay. Then I will give the point to kambiz. However, thanks to you millerw because your example encouraged me to try the solution which i've tried but thought was not working.
0
millerwCommented:
No problem.

Good luck and happy coding!
0
kambizCommented:
Thank you Samchan and thank you Millerw.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.