Link to home
Create AccountLog in
Avatar of a3dvm
a3dvm

asked on

Force date entry on new record

Hi Experts
Delphi app using Access db. ('s' prefixes are Alphaskin controls)
Trying to force entry of a date field before saving form. Already force entry of 'Last Name' field which works fine.
But two attempts at code I've added for the date/time picker (sDBDateEdit2) just get ignored, and the record saves.
There were no compile errors.
Any ideas of how to incorporate both sDBEdit3 and sDBDateEdit2 to both be required fields with message if null?

             // Save Button
procedure TFormNewCont.SaveContBtn2Click(Sender: TObject);
    begin
    // This code works...
    if FormNewCont.sDBEdit3.edittext = '' then
    sShowMessage('Last Name missing!', 'Last Name is a required field'+#13+#10'- please add Last Name') ;
    // This didn't work...
    //if FormNewCont.sDBDateEdit2.edittext = '' then
    // sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date');

    // Nor did this...
    if FormNewCont.sDBDateEdit2.CheckValidDate=true then
    sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date')

    else
    try
    datamodule1.dtContacts.post ;
    dataModule1.dtContacts.Insert;
    SaveContBtn2.Enabled:=False;
    AddAnotherBtn2.Enabled:=True;
    CancelBtn2.Enabled:=True;
    DataModule1.dtContacts.FieldByName('Name_Sal').FocusControl;
    except
    on E:Exception do DataModule1.No25ADOConn.RollbackTrans;
    end;
    end;
Thanks,
Den
Avatar of atul_parmar
atul_parmar
Flag of India image

BeforePost is better place to validate data. You can also use Field1.IsNull to check whether any data assigned or not.

Atul
Avatar of a3dvm
a3dvm

ASKER

Hi atul
I tried 'IsNull' before - but it gave me an 'undeclared identifier' error on compiling.
IsNull does not show up in the prompt list of actions after the field full stop - which is why I guess.
(I should explain I'm pretty new to Delphi...)
I'll look at the BeforePost suggestion after I can see it works first.
This is very simple example using TTable; and can you show your code where you get 'undeclared identifier'  error

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  if Table1.FieldByName('ID').IsNull then
  begin
    MessageDlg('ID can not be blank', mtInformation, [mbOk], 0);
    Abort;
  end;
end;
Avatar of a3dvm

ASKER

Hi Atul
I'm doing this in a form, hence "FormNewCont.sDBEdit3.edittext". To get your code to work I changed to the dataset - DataModule1.dtContacts.FieldByName('Wedding_Date').IsNull.
But - even when a date IS added, it still brings up the dialog message (Wedding Date Missing)
(please also see below...)
             // Save Button
procedure TFormNewCont.SaveContBtn2Click(Sender: TObject);
    begin
    if FormNewCont.sDBEdit3.edittext = '' then
    sShowMessage('Last Name missing!', 'Last Name is a required field'+#13+#10'- please add Last Name') ;
    if DataModule1.dtContacts.FieldByName('Wedding_Date').IsNull then
    sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add a Wedding Date')
    else
    try
    datamodule1.dtContacts.post ;
    dataModule1.dtContacts.Insert;
    SaveContBtn2.Enabled:=False;
    AddAnotherBtn2.Enabled:=True;
    CancelBtn2.Enabled:=True;
    DataModule1.dtContacts.FieldByName('Name_Sal').FocusControl;
    except
    on E:Exception do DataModule1.No25ADOConn.RollbackTrans;
    end;
    end;
I saw that your code Procedure referred to the data set. To get the 'BeforePost' call - I put your code in the DataModule I'm using. But the Abort step excepted with something about trying to roll back before a transaction?
Can I keep this code in the "procedure TFormNewCont.SaveContBtn2Click(Sender: TObject);" area as above - but find a way of losing the 'Wedding Date missing' when a date actually is entered please.
Den
Abort means raising a silent exception; your your code to rollback should be like
if DataModule1.No25ADOConn.InTransaction then
  DataModule1.No25ADOConn.RollbackTrans;
>>but find a way of losing the 'Wedding Date missing' when a date actually is entered please.
I don't have the controls you are using so can not say what's causing the problem.
See the other properties of your DateEdit control like Text, Date, Value, Time
Avatar of a3dvm

ASKER

I tried this in the data module:
procedure TDataModule1.dtContactsBeforePost(DataSet: TDataSet);
begin
     if dtContacts.FieldByName('Wedding_Date').IsNull then
  begin
    MessageDlg('Wedding Date can not be blank', mtInformation, [mbOk], 0);
    Abort;
    if DataModule1.No25ADOConn.InTransaction then
  DataModule1.No25ADOConn.RollbackTrans;
end;
end;
Gave EOLException - 'You tried to commit or rollback without first begining a transaction'
(the same happened with or without the 'if DataModule1.No25ADOConn.InTransaction' lines)

The controls I'm using are pretty much the std. Delphi ones with the addition of skins.
I think the reason the message kept appearing even with the date entered was because I was using your Data Module field (Table1.FieldbyName) which will be empty as I haven't posted at this stage.
Is there some way to test the the form field (which will be either empty or filled at this stage) as I have in the existing code for Last Name.
Neither of these brought up the message dialog when the field was empty:
    if FormNewCont.sDBDateEdit2.edittext = '' then
     sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date');
    if FormNewCont.sDBDateEdit2.CheckValidDate=true then
    sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date')

Is there some other way of testing the forms sDBDateEdit2 field other than the ".edittext =' ' " or ".CheckValidDate=true" extensions that I've tried?
ASKER CERTIFIED SOLUTION
Avatar of a3dvm
a3dvm

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer