Jump to content

How do you validate an ADOtable?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
30 replies to this topic

#1
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
So I have an ADOtable linked to my database. I can freely enter data in the fields, and I want to validate them.

What do I do to validate it? Usually when I validate a edit box, I use the OnkeyPress Event. But here, it can be any field..how would I find out which field it is in?

Would I say something like:

case Keypress.field of

Blah:
begin
validation
end;

blah2
begin
validation
end;

?

Since I have different validations for each field. Can someone confirm, thanks

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I wouldn't directly expose an ADOTable in your forum. Do the validation in the interface to it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
Come again? Sorry, I don't understand what you mean, laymen terms please >__>

#4
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
You can use the same event code for multiple controls. The "Sender" parameter is a pointer to the control that triggered the event.

If you have a OnKeyPress event for numbers, you can reuse the event for all TEdit controls that accept numbers.

I would also look into TMaskEdit and JEDI's TJvDBSpinEdit which handles validation for you.

#5
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Yes. May I add that it doesn't seem to make much sense to have 1 handler for all your edits and then handle them separately anyway inside that one handler?
Instead, give each edit its own handler!

Ps, to test the Sender use

If Sender=DBEdit1 then //cannot use case with pointers :)

 begin

  ...ValidateThis...;

   exit; //optional, uness you have a "default". Like this, your handler is left immediately

 end;


This assumes the edit you want to test is DBEdit1, but you can change that. You can also put more.
Whatever you like, I guess.

#6
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
0_o, sorry but I am only still A level standard >__>

I think I didnt describe the problem properly. I have a table and each column has its own validation, Ie, Postcode has string, and telephone number has only integers.

But they are part of the same DBGrid, so I was wondering how I would have each validation for each.

But, I figured a different method. Is it possible to have a situation where:

You select a row in the DBgrid to edit,

then I also have a combobox elsewhere on the form with all the header titles of the columns. Next to this I have a Editbox where I can change the field to whatever I want.

So I highlight the row, choose the column to edit, then type in the edit box, then click Ok. I'm gonna make the DBgrid read only, so nothing can be changed directly. This is possible right?

Or should I do it another more efficient method?

Thanks :P

#7
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Well, you could probable use a case then, based on a column index, but I am not very familiar with those DBGrid things. Good luck with it :thumbup1:.

#8
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
O, right. Gotcha. look into OnBeforePost

Quote

An application might use BeforePost to perform validity checks on data changes before committing them. If it encountered a validity problem, it could call Abort to cancel the Post operation (Delphi) or throw an exception (C++).

procedure TFormMain.TableBeforePost(DataSet: TDataSet);

begin

  with DataSet.FieldByName('N1') do

    if (Value < 1000) or (Value > 9999) then

      raise Exception.Create(FieldName + ' is invalid');


  with DataSet.FieldByName('N2') do

    if (Value < 0) or (Value > 100) then

      raise Exception.Create(FieldName + ' is invalid');

end;

Using this method will prevent any control from posting invalid data to the table. Also look into database validation. Most databases will have some kinda extended validation.

ie: Access has validation rules. Paradox (BDE) has some basic validation checks (min, max, masking). MySQL has even more.
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/

#9
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
I do not have Onbeforepost on my DBgrid Events list :S I tried checking on google, but I can't find anything on it apart from some high level computing >__>

#10
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
Its in TDataSet so it will be in your TADOTable.

Delphi OnBeforePost - Google Search

http://docwiki.embar...orePost_(Delphi)
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/

#11
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
Ah thanks, its working :D

Can I just ask for a validation for numbers? Like, I want to check if the entered data is a pure integer number. Maybe something like

If Value <> Iinteger

But this doesnt work :P

Thanks

EDIT. And also, if I have several columns that have the same validation, how do I put them into one? Ie :

 with Dataset.FieldByName('Gender') do
   begin
    Value:= Uppercase(String(Value));
      if (Value <> 'M') and (Value <> 'F') then
       begin
        Showmessage(FieldName + ' is invalid');
        Value:='';
       end;
   end; 

Actually, this may be a bad example, but if another column also had the same validation ( Either M or F), how would I do it? Do I have to do another whole lot of code (well, copy and paste) ?

Thanks

#12
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
You should be making integer fields of type "integer" so the database would handle the validation, but if you must check that a string is a valid integer, then use the StrToInt function which will throw an exception if the string is invalid:

  with DataSet.FieldByName('N1') do

  try

    StrToInt(Value);

  except

    on E: Exception do ShowMessage(FieldName + ' is Invalid.' + E.Message);

  end;

RE: the second question: if you have lots of validations, you should start up a library of functions that will validate fields:

procedure ValidateInteger(const AField: TField);

begin

  try

    StrToInt(AField.Value);

  except

    on E: Exception do ShowMessage(AField.FieldName + ' is Invalid.' + E.Message);

  end;

end;


procedure ValidateFloat(const AField: TField);

begin

  try

    StrToFloat(AField.Value);

  except

    on E: Exception do ShowMessage(AField.FieldName + ' is Invalid.' + E.Message);

  end;

end;


procedure ValidateIsCountry(const AField: TField);

begin

  if Pos(AField.Value + ',', 'Australia,USA,UK,') = 0 then Abort;

end;

Then just call the functions inside OnBeforePost for each field:

  ValidateInteger(DataSet.FieldByName('N1'));

  ValidateInteger(DataSet.FieldByName('N2'));

Simple. All your posts wiil be validated and you will have a solid database.

As a general rule, you should validate data at the database level. That way all clients connected to the database will be prevented from posting invalid records. Which is important for large scale developments.
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/