Topic: Avoid empty data being recorded

Hi all,

This time, I am looking for an easy script to avoid recording empty data with script as my columns do not have "NOT NULL" set.

example:

Form1

Textbox1                   buttonsave1


If Textbox1 does not contain anything when click on buttonsave1, I want to discard the save and display an alert.


The purpose is simple, I want to avoid any blank row being created which would have an impact with other specific mechanism in my project. Furthermore, there is no delete possibility as well on purpose.

Re: Avoid empty data being recorded

Hello,

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if Form1.Edit1.Text = '' then
    begin
        Cancel := True;
        ShowMessage('Your message here.');
    end;
end;
Dmitry.

Re: Avoid empty data being recorded

Thanks Dmitry, I was not sure about the syntax.

Re: Avoid empty data being recorded

hi Dmitry
I'm having the same problem as tcoton

you wrote this query for the answer to his question:

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if Form1.Edit1.Text = '' then
    begin
        Cancel := True;
        ShowMessage('Your message here.');
    end;
end;

suppose that I have a table named "ABC" and in the ABC table I have a column named "XYZ"
please tell me where should I put your above query?
thanks

Re: Avoid empty data being recorded

This script does not relate to a table but to a textbox.

In a new project, Form1 is the default form, if you create a textbox, default name is Edit1, if you create a button, default name is Button1.

By default, the scripts are not activated, click on the green icon (scripts) next to the green arrow to enable the script tab.

Then, if you created a textbox, go to the properties, click on "Events" tab and double click on the event "OnClick", you will be switched automatically to the script tab.

An empty procedure will be created with "begin" and "end". Fit the code in between those "blue" indicators.

This simple scripts says than if the text of Edit1 in Form1 is empty when click on Button1, then it will cancel saving the data and display the message defined in "ShowMessage".

I use a variant to display an alert with an icon: instead of ShowMessage, I use

MessageDlg('Your messahe here', mtError, mbOk, 0); // Displays a red 'X', header is: Error

6 (edited by identity 2016-01-22 19:59:36)

Re: Avoid empty data being recorded

thank you so much

I added your script and it worked fine
but by default, there is another script named " the script says hello"
when I start the file an alert comes up and says  " the script says hello"
I tried to delete the script but when I delete the script, It shows an error

the scrip is like this:

var
  sHello: string;

procedure frmTerminal_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if frmTerminal.Edit13.Text = '' then
    begin
        Cancel := True;
        MessageDlg('Message here', mtError, mbOk, 0); // Displays a red 'X', header is: Error;
    end;
end;

procedure HelloWorld (s: string);
begin
  ShowMessage(s);
end;

begin
  // just for example
  sHello := '';
  HelloWorld(sHello);
end.

how can I erase this script?

Re: Avoid empty data being recorded

Delete everything and leave this:

procedure frmTerminal_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if frmTerminal.Edit13.Text = '' then
    begin
        Cancel := True;
        MessageDlg('Message here', mtError, mbOk, 0); // Displays a red 'X', header is: Error;
    end;
end;


begin

end.

What you delete is actually the part of the Hello script.

Re: Avoid empty data being recorded

Hi
your script works fine and thanks for your reply

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    if Form1.Edit1.Text = '' then
    begin
        Cancel := True;
        ShowMessage('Fill in the Blanks.');
    end;
end;

but in my project apart form the save button, there is also a button defined as "New Record"
whenever I push the save button, the message ('Fill in the Blanks.') is shown and that's OK
But when I press the "New Record" button, this message is also shown. What should I do to get rid of seeing this message when I press the "New Record" button
thanks

Re: Avoid empty data being recorded

I guess your button "New Record" name is button1, you must create a button save which will save your data and set the procedure on this button which might be button2 or button3 whatsoever...

10 (edited by identity 2016-01-23 04:29:05)

Re: Avoid empty data being recorded

Hi tcoton

I have checked my project but button "New Record" name is button2 and button "save" name is button1
the script says that only button 1 should show the message but both of the buttons show the message.

I have attached a simple version of the project which is somehow like my project. can you correct it?

thanks

Post's attachments

Attachment icon Linked Lists.rar 293.03 kb, 636 downloads since 2016-01-23 

Re: Avoid empty data being recorded

Hi,

I have modified your project, there was a misuse of the function "New Record".

I removed your "New" button from your "add info" form and set "New Record" to "Add" on main form.

Post's attachments

Attachment icon Linked Lists.zip 324.33 kb, 635 downloads since 2016-01-29 

12 (edited by identity 2016-01-29 20:58:37)

Re: Avoid empty data being recorded

thanks but you did not correct the bug, you just erased it.
The Whole idea was to correct the project without changing it because in form1 of my project there is a save button and without filling the text1 button you can't save it. but there is also a Table Grid in that form and TableGrid has a New Record Button which opens form2.
the error is:
only when you press Save Button the message "Fill in the blank" should appear but when you press the New Record Button the message still appears
I attach a simple version of my project. please do not delete the buttons, just correct the error of the "New Record Button"

Post's attachments

Attachment icon Linked Lists.rar 292.35 kb, 650 downloads since 2016-01-29 

Re: Avoid empty data being recorded

I think you did not understand what "New Record" means, hence the issue. You may have to work on the design of your project as the function "New Record" means that you open a new record with a new id ready for inserting a new row. When you save, you save whether a new record or an update of your record.

14 (edited by identity 2016-01-30 05:04:33)

Re: Avoid empty data being recorded

Hi
I know what "New Record" means. there is a project in Database Application of this forum named "Student Performance Database" and I also attached it for you.
In the form "frmStudent" there is a save button and it also has a Table Grid and the Table Grid has an "Add" Button
could you please write an script that when you press the save button without filling "edLastname" , a message is shown that says "Fill in the last name".
Only when the save button is clicked, this message should be shown not when you press "Add" Button.
thanks

Post's attachments

Attachment icon Students.rar 298.68 kb, 612 downloads since 2016-01-30 

Re: Avoid empty data being recorded

Hello identity

To avoid a field is emty, when creating your database and its fields, you could check property Not Null (behind +Additional) when you define field LastName.

Otherwise, if you show a message telling users they have not filled this field, you could use this script :

Procedure frmStudent_bSave_AfterClick(Sender: string);
Var  Cancel : boolean;
Begin
   If frmStudent.edLastName.Text='' then
   begin
        ShowMessage('You have to fill this field');
       Cancel := True;
   end;
end;

This script is to place behind OnAfterClick event for bSave button as you want it.

IS it OK for you ?

JB

Re: Avoid empty data being recorded

Hi
I did as you said but it got worse than before
when I leave "edLastName" Text Box blank, the message comes up and when I click Ok, the form "frmStudent" closes.
and also the first error still exists.
when I click on the "Add" button in the Table Grid in the  "frmStudent" , this message is also shown.
what is the reason?

Re: Avoid empty data being recorded

You are right, I could reproduce the issue using MVDB 2.2 and the example project student and I think it is a bug.

It is like if the Action "New Record" was triggering the button save in frmStudent for some reason!

This is to address to Dmitry smile

@Identity - Sorry for my previous answers, I never faced this issue as I am using another way to check empty fields wink

Re: Avoid empty data being recorded

thanks for your reply
Could you please elaborate on you another way of checking empty fields?
are there any other ways to do it?

Re: Avoid empty data being recorded

Hello identity

Strange
My script works fine.
Please see attachment;

JB

PS : after reading tcoton's post, I'm worling with MVD 2.23

Post's attachments

Attachment icon Students.zip 15.86 kb, 676 downloads since 2016-01-30 

20 (edited by identity 2016-01-30 20:07:32)

Re: Avoid empty data being recorded

Hi jean.brezhonek
Yes Very Strange
mine does not work correctly. I have downloaded your attachment and it also has the same problem. I'm working with 1.45 and 2.3 portable.
both of them are the same. In the form "frmstudent" when I press add the message"you have to fill the blanks" is shown and when I click on "OK" then the other form comes

Re: Avoid empty data being recorded

Hello everyone

the script for empty text fields is excellent!
but in case of a combobox?
if I do not select any data from the list of a combobox
how could i apply a script?

I really appreciate your help and time.

best regards to all!!

Re: Avoid empty data being recorded

If (form1.combobox1.dbitemid = -1) then
Begin
          ShowMessage('please fill required field');
          Cancel:= true;
End;
You can write it in different ways
Like combobox1.text = '' or combobox1.dbitemid < 1

Re: Avoid empty data being recorded

Thank you very much blackpearl8534 it works perfectly

Thanks for your time
regards

Re: Avoid empty data being recorded

Hello, good day to everyone

I am dealing with a DateTimePicker don't forget to select when saving
but there is no action, I think the script is wrong
Your support would be of great help in guiding me.



begin
if Analysis_Report.DateTimePicker2.Checked  then
begin
Cancel :=True;
ShowMessage('Message here');
end;


Thank you very much for your time and support.

Best regards

Re: Avoid empty data being recorded

Hi Eserjbrujo,
Try attaching your code to an 'on save' event rather than the datetimepicker2.checked event.
There are a number of different ways to check whether something has been filled in (see attached).
1.  Force Date 1 (in the attachment);  make the field in question mandatory and it is not possible to save until it has been entered (no script required).
2.  Force Date 2 (in the attachment);  make the 'save button' invisible until the required field(s) have been filled in.
3.  Force Date 3 (in the attachment);  using 'showmessage' (you could also try 'messagebox' which is perhaps more user-friendly).
Regards,
Derek.

Post's attachments

Attachment icon force date.zip 1010.51 kb, 246 downloads since 2022-06-29