Board index » delphi » Using filters in BeforePost event handler?

Using filters in BeforePost event handler?

Hi,

I'm writing a C++Builder 3.0 database application. I have a TTable,
"Userstable", containing a user database, with several boolean flag
fields for each user. This table is editable by the user using
data-aware controls.

I want to ensure that there is always at least one record where one
particular flag, "Admin", is set to true. Thus, if the user toggles
off the Admin flag for a user record, I want to prevent the change
from being written if there will be no records with Admin==true after
the change is posted.

What I tried to do is to create a BeforePost handler for Userstable,
and then use a filter to filter out the current record as well as all
other records with Admin==true, and then signal an error if table's
RecordCount is zero after that. The code looks something like this:

  // ... In BeforePost handler ...
  // UserstableUserCode is the primary key field object of Userstable
  AnsiString UserCode = UserstableUserCode->Value;
  int CurRec = Userstable->RecNo;
  Userstable->Filter = "(Admin='true') and (UserCode<>'"
                        + UserCode + "')";
  Userstable->Filtered = true;
  int AdminRecCount = Userstable->RecordCount;
  Userstable->Filtered = false;
  Userstable->RecNo = CurRec;
  if(!AdminRecCount) {
    // .. error msg ..
    Abort();
  }
  // etc...

Unfortunately, running this code causes a Stack Overflow error. I
suspect it's because the very act of setting the Filter will itself
call Post().

However, I can't figure out what's the best way to achieve what I want
to do! If you have any suggestions for me, please let me know as soon
as possible! Thanks very much!

P.S. Although I happen to be using C++Builder in this case, any
relevant Delphi code examples or references would be perfectly fine as
well.

kelvin lim

 

Re:Using filters in BeforePost event handler?


: kelvin...@post.com (Kelvin Lim) wrote:

Quote
>I'm writing a C++Builder 3.0 database application.

Hello, please do not cross- or multi-post; people are not really fond of
this and it leads to all sorts of inefficiencies and confusion - major
problems, in other words.

The newsgroups guidelines (see http://www.borland.com/newsgroups/)
specifically mention this case; you might want to check them out.

I have cancelled (deleted) your message because of your cross-posting.
Please repost your message to one group ONLY; I would suggest:

  borland.public.cppbuilder.database.desktop

Followup-to is set to borland.public.cppbuilder.non-technical

Re:Using filters in BeforePost event handler?


Hi,

I'm writing a C++Builder 3.0 database application. I have a TTable,
"Userstable", containing a user database, with several boolean flag
fields for each user. This table is editable by the user using
data-aware controls.

I want to ensure that there is always at least one record where one
particular flag, "Admin", is set to true. Thus, if the user toggles
off the Admin flag for a user record, I want to prevent the change
from being written if there will be no records with Admin==true after
the change is posted.

What I tried to do is to create a BeforePost handler for Userstable,
and then use a filter to filter out the current record as well as all
other records with Admin==true, and then signal an error if table's
RecordCount is zero after that. The code looks something like this:

  // ... In BeforePost handler ...
  // UserstableUserCode is the primary key field object of Userstable
  AnsiString UserCode = UserstableUserCode->Value;
  int CurRec = Userstable->RecNo;
  Userstable->Filter = "(Admin='true') and (UserCode<>'"
                        + UserCode + "')";
  Userstable->Filtered = true;
  int AdminRecCount = Userstable->RecordCount;
  Userstable->Filtered = false;
  Userstable->RecNo = CurRec;
  if(!AdminRecCount) {
    // .. error msg ..
    Abort();
  }
  // etc...

Unfortunately, running this code causes a Stack Overflow error. I
suspect it's because the very act of setting the Filter will itself
call Post().

However, I can't figure out what's the best way to achieve what I want
to do! If you have any suggestions for me, please let me know as soon
as possible! Thanks very much!

P.S. Although I happen to be using C++Builder in this case, any
relevant Delphi code examples or references would be perfectly fine as
well.

kelvin lim

Re:Using filters in BeforePost event handler?


On Sat, 21 Aug 1999 14:32:44 GMT, kelvin...@post.com (Kelvin Lim)
wrote:

Quote
>Hi,

>I'm writing a C++Builder 3.0 database application. I have a TTable,
>"Userstable", containing a user database, with several boolean flag
>fields for each user. This table is editable by the user using
>data-aware controls.

>I want to ensure that there is always at least one record where one
>particular flag, "Admin", is set to true. Thus, if the user toggles
>off the Admin flag for a user record, I want to prevent the change
>from being written if there will be no records with Admin==true after
>the change is posted.

Why not let the database do the validation. A before update trigger
could use a stored procedure to determine the number of Admin entries
and throw an exception if the update would set the number of Admin
entries to zero.

please don't crosspost

regards
A.G.

Re:Using filters in BeforePost event handler?


Quote
>What I tried to do is to create a BeforePost handler for Userstable,
>and then use a filter to filter out the current record as well as all
>other records with Admin==true, and then signal an error if table's
>RecordCount is zero after that. The code looks something like this:

Use a second copy of the Ttable to do this

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads