Jump to content
Ian Branch

Is this a change in 12.3??

Recommended Posts

Hi Team,

I may have beem wrong from the beginning but...

In a calling form I have created a Sub Form then set some parameters for the form.

In the sub form I had this:

  private
    const
      iWidth = 865;
    var
      { Private Declarations }
      FiQty: Integer;
      FsFileName: string;
      FsQuery: string;
      FsTitle: string;
  public
    { Public Declarations }
    property iQty: Integer read FiQty write FiQty;
    property sFileName: string read FsFileName write FsFileName;
    property sQuery: string read FsQuery write FsQuery;
    property sTitle: string read FsTitle write FsTitle;
  end;

In the form's type.

I then had this in the Form Create:

procedure TStatsGridForm.FormCreate(Sender: TObject);
begin
  //
  Caption := sTitle + ' - Total records..' + IntToStr(iQty);
  JobTickets.SessionName := dmC.DBC1.SessionName;
  JobTickets.SQL.Clear;
  JobTickets.SQL.Add(sQuery);
  JobTickets.Prepare;
  JobTickets.Open;
  //
....
....

D12.3 doesn't seem to like this as it says that there is no SQL Query when being created. But it's been working for years.

Any way, I moved the code to OnShOw and it is now happy and working:

procedure TStatsGridForm.FormShow(Sender: TObject);
begin
  //
  Caption := sTitle + ' - Total records..' + IntToStr(iQty);
  //
  JobTickets.SQL.Clear;
  JobTickets.SQL.Add(sQuery);
  JobTickets.Prepare;
  JobTickets.Open;
  //
end

So, to the question.  Was the original way allways wrong and I was just lucky, or, has there been a change??

I use this technique in several areas so I may need to re-visit them all.. 😞

 

Ian

Share this post


Link to post

Is the sub-form auto-created when the application starts?  If so, Create is likely being called before you have a chance to set sQuery.

Share this post


Link to post

No.  The calling form creates it just before setting the parameters.

Here is the calling form's code:

procedure TStatsForm.edtTotalInClick(Sender: TObject);
begin
  //
  if edtTotalIn.AsInteger = 0 then Exit;
  //
  var sStartDate := FormatDateTime('yyyy-mm-dd', edtStartDate.AsDate);
  var sFinishDate := FormatDateTime('yyyy-mm-dd', edtFinishDate.AsDate);
  var sJobType := Trim(edtJobType.Text);
  if sJobType.IsEmpty then sJobType := '%';
  var StatsGridForm := TStatsGridForm.create(nil);
  //
  StatsGridForm.sFileName := 'JTsInRaised';
  StatsGridForm.sTitle := 'Job Tickets In/Raised..';
  StatsGridForm.sQuery := Format('''
                Select JobNo, CustomerRef, Manufacturer, Model, AccountCode, JobType, JobStatus, DateIn,
                DateRepaired, DateOut, DateInvoiced, Hours, RepairCode, Deposit, Charge from JobTickets
                where BusCode = %s  and DateIn Between date %s and date %s and jobType Like %s
                ''',[QuotedStr(AUD.BusCode), QuotedStr(sStartDate), QuotedStr(sFinishDate), QuotedStr(sJobType)]);
  StatsGridForm.iQty := edtTotalIn.AsInteger;
  //
  try
    StatsGridForm.ShowModal;
  finally
    StatsGridForm.Free;
  end;
  //
end;

StatsGridForm being the sub form.

Share this post


Link to post
3 minutes ago, Ian Branch said:

var StatsGridForm := TStatsGridForm.create(nil);

...

StatsGridForm.sQuery := Format(''' ...

Right there:  You are creating the form (which calls FormCreate) right before you set sQuery; therefore, sQuery is blank in FormCreate!

 

Are you sure this was working exactly this way in previous versions of Delphi?

 

6 minutes ago, Ian Branch said:

Hmmm... Title should read:  Is this a change in 12.3??

Yeah, I was wondering about that. :classic_wink:

Share this post


Link to post

Hi David,

I had a feeling that was the case, but yes, it has been working for years.

I shall have to address other implementations like this.  No biggie, just a pain in the proverbial.

Share this post


Link to post
26 minutes ago, corneliusdavid said:

If so, Create is likely being called before you have a chance to set sQuery.

I'm going to correct myself before someone else does:  I was thinking that maybe sQuery was being passed in or somehow set before it was created but since it's a private variable of the form being created, it couldn't possible be set before it was created.  DUH!

Share this post


Link to post
9 minutes ago, Ian Branch said:

yes, it has been working for years.

Very strange. Not sure how that's ever worked--unless you were overriding the constructor or calling BeforeConstruction or AfterConstruction. The values from the DFM are streamed in and set after it's created and before FormCreate is called so that's the only other way values can be set before the event handler is fired but according to the code you showed, that's not how sQuery is being set.

Share this post


Link to post
1 minute ago, corneliusdavid said:

Very strange. Not sure how that's ever worked

In retrospect,me either.  But, I have it right now.  🙂

Thank you for your feedback.  Appreciated.

Share this post


Link to post

Always take care to put things in onShow. That can be called more often than you wish, unlike onCreate.

Edited by Die Holländer

Share this post


Link to post

In this situation I would pass the parameters to the onFormCreate procedure.

 

Quote

sQuery := Format('''
                Select JobNo, CustomerRef, Manufacturer, Model, AccountCode, JobType, JobStatus, DateIn,
                DateRepaired, DateOut, DateInvoiced, Hours, RepairCode, Deposit, Charge from JobTickets
                where BusCode = %s  and DateIn Between date %s and date %s and jobType Like %s
                ''',[QuotedStr(AUD.BusCode), QuotedStr(sStartDate), QuotedStr(sFinishDate), QuotedStr(sJobType)]);

StatsGridForm := TStatsGridForm.create(self, 'JTsInRaised','Job Tickets In/Raised..',sQuery,edtTotalIn.AsInteger);
try
  StatsGridForm.ShowModal;
finally
  StatsGridForm.Free;
end;

 

 

procedure TStatsGridForm.FormCreate(Sender: TObject,..,..,aQuery);
begin
  fQuery:=aQuery
  ....
  JobTickets.SQL.Clear;
  JobTickets.SQL.Add(fQuery);
  JobTickets.Prepare;
  JobTickets.Open;
  ...
end;

 

Edited by Die Holländer

Share this post


Link to post
25 minutes ago, Die Holländer said:

In this situation I would pass the parameters to the onFormCreate procedure.

That won’t work as you describe. You can't add parameters to the OnCreate event. What you are proposing requires defining a new constructor instead.

Share this post


Link to post
3 hours ago, corneliusdavid said:

The values from the DFM are streamed in and set after it's created and before FormCreate is called

Except in this case, as the properties shown are not declared as published, so not streamed by the DFM at all.

Share this post


Link to post

You could use a setter for sQuery, that sets up the SQL before showing the form.

That would not nee FormShow.

  • Like 1

Share this post


Link to post
6 hours ago, Remy Lebeau said:

Except in this case, as the properties shown are not declared as published, so not streamed by the DFM at all.

Right. I mentioned that as the only other way that properties could be set before FormCreate was called. To do that of course, he'd have to set a published label or editbox (or some component) at design-time.

Share this post


Link to post
public
  Constructor Create(aOwner: TComponent; aQty:Integer; aFileName:string; aQuery:String; aTitle:string);reintroduce;
end;

procedure TStatsGridForm.Create(aOwner:TComponent; aQty:Integer ;aFileName:string; aQuery:String; aTitle:string);
begin
  FsQuery:=aQuery;
  FiQty:=aQty;
  FsFileName:=aFileName;
  FsTitle:=aTitl;

  Caption := FsTitle + ' - Total records..' + IntToStr(FiQty);

  JobTickets.SQL.Clear;
  JobTickets.SQL.Add(FsQuery);
  JobTickets.Prepare;
  JobTickets.Open;
  ...
end;

 

Edited by Die Holländer

Share this post


Link to post
12 minutes ago, Die Holländer said:

procedure TStatsGridForm.Create(aOwner:TComponent; aQty:Integer ;aFileName:string; aQuery:String; aTitle:string);
begin
  ...
end;

 

Don't forgot to call 'inherited Create(aOwner);'

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×