Summary: one form (Loan Form) dynamically creates a modal form called DatePickerForm (when user clicks a specific button). After selecting a date in the DatePickerForm, the user clicks on that form's 'Close' button: (a BitBtn) - this is what causes an access violation error.
Details:
The purpose of the reusable modal DatePickerForm is to provide users with a consistent way of entering dates in special circumstances. It will be used in multiple other situations - that is, if I get it to work as planned.
Exact error text is: "Project ABCD.exe raised exception class $C0000005 with message 'access violation at 0x0060d0b1: read of address 0x00000000'."
The code compiles and the program works fine until step 4 below:
Run-time Process:
- The user clicks on a button on the Loan form (works)
- The modal form DatePickerForm is created (owner: Application), then shown. (works)
- The user selects a date from the DatePicker control. (works)
- The User clicks on the OK button (fails)
- The DatePickerForm should close and we should return to the Loan form - but the error occurs instead.
- The next step would be reading the date still on the DatePicker's form DatePicker control (the form still exists, it is just invisible at this point)
My questions:
A) Should this work or am I using dynamic form creation incorrectly?
B) Is there a better way to achieve this?
Any help will be appreciated.
John
DatePickerForm code (complete):
unit DatePicker_PopupForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ComCtrls;
type
TfmDatePicker_Popup = class(TForm)
DTDatePicker: TDateTimePicker;
lblDatePrompt: TLabel;
btnOK: TBitBtn;
procedure btnOKClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fmDatePicker_Popup: TfmDatePicker_Popup;
implementation
{$R *.dfm}
procedure TfmDatePicker_Popup.btnOKClick(Sender: TObject);
begin
fmDatePicker_Popup.CloseModal;
end;
end.
Loan form - partial code (complete code is roughly 9700 lines long)
unit LoanForm;
interface
uses
Winapi.Windows, ......, DatePicker_PopupForm;
...
implementation
...
procedure TfmLoan.btnSetDefaultClick(Sender: TObject);
begin
DatePickerForm := TfmDatePicker_Popup.Create(Application);
DatePickerForm.DTDatePicker.Date := GD_ProcessDate;
DatePickerForm.ShowModal;
dDefaultDate := DatePickerForm.DTDatePicker.Date;
end;
...
end.