Topic: I want to limit numbers of records

I am trying to create simple database with two forms, one table  and one datagrid in first form.
I want to limit numbers of records that can be saved by user.
The user is not allowed to save a new record if Datagrid in Form1 already contains a certain number of records, eg 100. And the corresponding message appears.
Is there reliable script to do that?
Thanks in advance.

2 (edited by derek 2020-08-13 23:02:43)

Re: I want to limit numbers of records

Hi Markos,
Just a suggestion but it might be more user friendly to stop the user from being able to add a record BEFORE any data has been entered rather than stop the user from saving a record AFTER the data has been entered.
Please see attached (you can set the maximum number of rows in Form3)
Derek.

Post's attachments

Attachment icon limitrows.zip 336.79 kb, 301 downloads since 2020-08-13 

Re: I want to limit numbers of records

Thanks, Derek,
But I want the form1.button4 and form3 to be invisible for the user all the time.
I tried with 3 options   but nothing is working:

   procedure Form1_OnShow (Sender: TObject; Action: string);
          begin

                Form1.Button4.Visible := False
          //    Form1.Button4.Enabled := False;
             //  Form3.Visible := False


          end;

4 (edited by MarkoS 2020-08-14 04:30:57)

Re: I want to limit numbers of records

OK.
I found in button4 properties.
What about the using Limit property of Datagrid1?

Re: I want to limit numbers of records

Thanks, Derek,
Your suggested solution sometimes works sometimes it doesn’t.
In short, sometimes I am not able to change the limit in form3.
Unfortunately, that happens with almost everything I tried to do in the MVD.
Very weird but sometimes it works, sometimes it doesn’t and I’m close to giving up the MVD altogether.
Please, explain me, is there any reason for this simple solution below to be incorrect?
(Form4 is instead of message.)
Thanks in advance.

     begin
  if form1.tablegrid1.rowcount >= 10  then form4.show  else form2.newrecord;
end;

6 (edited by derek 2020-08-14 16:25:19)

Re: I want to limit numbers of records

The solution I suggested should always work.
.
I suspect what might be happening is as follows:
a)  form3 uses an editable grid (it's quick and easy for the purposes of updating the table row limit).
b) when you change the table row limit, you MUST hit ENTER for your changes to be saved - don't just change it and quit out of the form.
If you sometimes forget to hit 'ENTER' then it will appear that the table row limit has not been changed.
Other than this, I can see no reason why this wouldn't always work. 
If this is a problem, change form3.tablegrid1 to be non-editable and add a maintenance form so you can change the table row limit.
Alternatively, if you are happy to hard-code the table row limit in the script, then simply delete form3 and any associated code.
.
You cannot use the 'limit' property of the tablegrid for what (I think) you are trying to achieve.  All this does is limit the number of rows that you can DISPLAY at one time - it does not limit the number of rows that you can SAVE.  Have a look at 'tablegrid display limit' in the attachment - there are 22 rows in the table but the tablegrid 'limit' property means that they are displayed in blocks of 10 - use the '>' and '<' to scroll up and down.
.
There is no reason why your solution below will not work (see 'tablegrid save limit' in the attachment).
begin
   if form1.tablegrid1.rowcount >= 10  then form4.show  else form2.newrecord;
end;
Derek.

Post's attachments

Attachment icon tablegrid limits.zip 675.05 kb, 301 downloads since 2020-08-14 

7 (edited by MarkoS 2020-08-15 04:30:21)

Re: I want to limit numbers of records

OK. Thanks, Derek.
If I am going to use the code mentioned then I can delete Form3 and tablelimit.
Am I right?
BTW, I am just curious why this code below is not working?
(if button4 visibility is true, selected in properties).

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
Form1.Button4.Visible := False
end;

Re: I want to limit numbers of records

Yes, that is correct.  If you hard code the limit in the script, you do not need Form3 nor the 'tablelimit' table.
Your code should have a semi-colon (;) at the end of the instruction, as follows
procedure Form1_OnShow (Sender: TObject; Action: string);
begin
Form1.Button4.Visible := False;
end;
The other possible reason why your code is not working is because you have not created the form1.onshow event for the form1 object (see the screenshot.jpg in the attachment).
As always, if you attach your project, it's easier to see what the problem is.
Derek.

Post's attachments

Attachment icon invisible button.zip 457.98 kb, 286 downloads since 2020-08-15