This topic is locked

Limit number of new child records that can be created

8/7/2010 5:38:02 PM
PHPRunner General questions
F
FunkDaddy author

Does anyone know how I can limit the number of child records that a user is allowed to created based on a field value found in the Master table?
For example:
I have a master record with a field called "New_Members", which is designed to take a integer value. I would like to limit the number of child records that can be inserted with the Master record based on what the user inputs in that field. So if user enters "5" in the "New_Members" field, then only 5 new child records could be created on that same page.
By the way... the context of this question assumes that a "add page" or add new record action is taking place with a master/child table... which means the form values are not already stored in the DB.
I did find a post that helps with this problem, but its not what I am looking for: http://www.asprunner.com/forums/topic/9883-limit-number-of-records/pagep34074hllimitfromsearch1&#entry34074
What I need is to add page to warn me right away once the user attempts to created more than the allowed number of child records. Remember, the child records are always shown as "inline add" when adding them via a master/child add new page form... thus, it would be nice to have a message pop-up in that child section when user clicks on "Inline Add" button (assuming they exceed the quantity as established on the Master section of the form).
Cheers,

A
ann 8/11/2010

Hi,
use Before record added event on the Events tab.

Here is a sample:

global $strTableName;

if ($_SESSION[$strTableName."_masterkey1"])

{

$rstmp = CustomQuery("select count(ID) as c_ID from DetailTable where DetailKey=".$_SESSION[$strTableName."_masterkey1"]);

$datatmp = db_fetch_array($rstmp);

$rstmp2 = CustomQuery("select New_Members from MasterTable where MasterKey=".$_SESSION[$strTableName."_masterkey1"]);

$datatmp2 = db_fetch_array($rstmp2);

if ($datatmp["c_ID"]>=$datatmp2["New_Members"])

{

$message = "your error message here";

return false;

}

else

return true;

}

return true;



where ID, DetailKey and MasterKey are your actual field names, DetailTable and MasterTable are your actual table names./

F
FunkDaddy author 8/11/2010

Thanks... but it didn't work. I don't understand why since the code you wrote does look very logical and makes perfect sense.
Is this happening because the child records have technically not yet been recorded in the database tables and therefore cannot be counted? Does the "Before Add" event take this into account?
It wouldn't make sense to add it in the "After Add" even either, since it would be too late to stop the record from being saved - right?
Looks like a timing issue of some sort.... but I could be completely wrong.
I look forward to finding the right answer on this one!
Cheers,