Get rid of Form2 altogether.
Instead, add a datamodule containing a datasource, a TZConnection, and a TZTable, and set their properties and hook them up correctly.
Add the datamodule unit to the uses of your main Form1 unit.
Then add code something like the following to your TForm1 class:
unit mainDynamicTabsheet;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Forms, Controls, ComCtrls, Menus, DBGrids, uDataModule;
type
{ TForm1 }
TForm1 = class(TForm)
AddTabsheetMenuItem: TMenuItem;
PageControl1: TPageControl;
PopupMenu1: TPopupMenu;
procedure AddTabsheetMenuItemClick(Sender: TObject);
private
TableTabsheet: TTabSheet;
TabSheetGrid: TDBGrid; // you would want a TDBVirtualGrid here with the appropriate Zeos unit in the uses clause
procedure ShowTableTabsheet(const aCaption: string);
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.AddTabsheetMenuItemClick(Sender: TObject);
begin
ShowTableTabsheet('Your caption goes here');
if Assigned(DataModule1.DataSource1.DataSet) then
DataModule1.DataSource1.DataSet.Active:=True;
end;
procedure TForm1.ShowTableTabsheet(const aCaption: string);
begin
if (TableTabsheet = nil) then begin
TableTabsheet:=PageControl1.AddTabSheet;
with TableTabsheet do begin
Caption:=aCaption;
TabSheetGrid:=TDBGrid.Create(TableTabsheet); // TDBVirtualGrid.Create in your case
TabSheetGrid.Align:=alClient;
TabSheetGrid.DataSource:=DataModule1.DataSource1;
TabSheetGrid.Parent:=TableTabsheet;
end;
end;
end;
end.