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.SaveContBtn2C lick(Sende r: TObject);
begin
// This code works...
if FormNewCont.sDBEdit3.editt ext = '' then
sShowMessage('Last Name missing!', 'Last Name is a required field'+#13+#10'- please add Last Name') ;
// This didn't work...
//if FormNewCont.sDBDateEdit2.e dittext = '' then
// sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date');
// Nor did this...
if FormNewCont.sDBDateEdit2.C heckValidD ate=true then
sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date')
else
try
datamodule1.dtContacts.pos t ;
dataModule1.dtContacts.Ins ert;
SaveContBtn2.Enabled:=Fals e;
AddAnotherBtn2.Enabled:=Tr ue;
CancelBtn2.Enabled:=True;
DataModule1.dtContacts.Fie ldByName(' Name_Sal') .FocusCont rol;
except
on E:Exception do DataModule1.No25ADOConn.Ro llbackTran s;
end;
end;
Thanks,
Den
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.SaveContBtn2C
begin
// This code works...
if FormNewCont.sDBEdit3.editt
sShowMessage('Last Name missing!', 'Last Name is a required field'+#13+#10'- please add Last Name') ;
// This didn't work...
//if FormNewCont.sDBDateEdit2.e
// sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date');
// Nor did this...
if FormNewCont.sDBDateEdit2.C
sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date')
else
try
datamodule1.dtContacts.pos
dataModule1.dtContacts.Ins
SaveContBtn2.Enabled:=Fals
AddAnotherBtn2.Enabled:=Tr
CancelBtn2.Enabled:=True;
DataModule1.dtContacts.Fie
except
on E:Exception do DataModule1.No25ADOConn.Ro
end;
end;
Thanks,
Den
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.
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(Da taSet: TDataSet);
begin
if Table1.FieldByName('ID').I sNull then
begin
MessageDlg('ID can not be blank', mtInformation, [mbOk], 0);
Abort;
end;
end;
procedure TForm1.Table1BeforePost(Da
begin
if Table1.FieldByName('ID').I
begin
MessageDlg('ID can not be blank', mtInformation, [mbOk], 0);
Abort;
end;
end;
ASKER
Hi Atul
I'm doing this in a form, hence "FormNewCont.sDBEdit3.edit text". To get your code to work I changed to the dataset - DataModule1.dtContacts.Fie ldByName(' Wedding_Da te').IsNul l.
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.SaveContBtn2C lick(Sende r: TObject);
begin
if FormNewCont.sDBEdit3.editt ext = '' then
sShowMessage('Last Name missing!', 'Last Name is a required field'+#13+#10'- please add Last Name') ;
if DataModule1.dtContacts.Fie ldByName(' Wedding_Da te').IsNul l then
sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add a Wedding Date')
else
try
datamodule1.dtContacts.pos t ;
dataModule1.dtContacts.Ins ert;
SaveContBtn2.Enabled:=Fals e;
AddAnotherBtn2.Enabled:=Tr ue;
CancelBtn2.Enabled:=True;
DataModule1.dtContacts.Fie ldByName(' Name_Sal') .FocusCont rol;
except
on E:Exception do DataModule1.No25ADOConn.Ro llbackTran s;
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.SaveContBtn2C lick(Sende r: TObject);" area as above - but find a way of losing the 'Wedding Date missing' when a date actually is entered please.
Den
I'm doing this in a form, hence "FormNewCont.sDBEdit3.edit
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.SaveContBtn2C
begin
if FormNewCont.sDBEdit3.editt
sShowMessage('Last Name missing!', 'Last Name is a required field'+#13+#10'- please add Last Name') ;
if DataModule1.dtContacts.Fie
sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add a Wedding Date')
else
try
datamodule1.dtContacts.pos
dataModule1.dtContacts.Ins
SaveContBtn2.Enabled:=Fals
AddAnotherBtn2.Enabled:=Tr
CancelBtn2.Enabled:=True;
DataModule1.dtContacts.Fie
except
on E:Exception do DataModule1.No25ADOConn.Ro
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.SaveContBtn2C
Den
Abort means raising a silent exception; your your code to rollback should be like
if DataModule1.No25ADOConn.In Transactio n then
DataModule1.No25ADOConn.Ro llbackTran s;
>>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
if DataModule1.No25ADOConn.In
DataModule1.No25ADOConn.Ro
>>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
ASKER
I tried this in the data module:
procedure TDataModule1.dtContactsBef orePost(Da taSet: TDataSet);
begin
if dtContacts.FieldByName('We dding_Date ').IsNull then
begin
MessageDlg('Wedding Date can not be blank', mtInformation, [mbOk], 0);
Abort;
if DataModule1.No25ADOConn.In Transactio n then
DataModule1.No25ADOConn.Ro llbackTran s;
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.In Transactio n' 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.e dittext = '' then
sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date');
if FormNewCont.sDBDateEdit2.C heckValidD ate=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?
procedure TDataModule1.dtContactsBef
begin
if dtContacts.FieldByName('We
begin
MessageDlg('Wedding Date can not be blank', mtInformation, [mbOk], 0);
Abort;
if DataModule1.No25ADOConn.In
DataModule1.No25ADOConn.Ro
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.In
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.e
sShowMessage('Wedding Date missing!', 'Wedding Date is a required field'+#13+#10'- please add Wedding Date');
if FormNewCont.sDBDateEdit2.C
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Atul