• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Record validation - Where is the appropriate place to do this?

Using Delphi 7 - MS Access - ADO


I have an application which uses NO data aware components.


When inserting or editing a record, the user is presented with a dialog box (again, no data aware components - all standard).

If inserting a record - all controls are cleared in the dialog box and the focus is set to the first control.
If editing a record - all controls are filled with the current selected record in the grid and the focus is set to the first control.


What i need to do is the following:

WHen inserting a record, I need to check to see if that record currently exists before allowing the post to continue. If it already exists, then prevent the dialog from closing and set focus back to the first control.

When Editing a record, I need to check to see if that record currently exists before allowing the post to continue. If it already exists, then prevent the dialog from closing and set focus back to the first control.


Here is what I currently do.

procedure TfrmChild.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 if frmData.tblChildren.Locate('ParentId;Name', VarArrayOf([frmData.tblParents.Fields[0].asInteger,
                             edtName.Text]),[])  then
 begin
  CanClose:= False;
  edtName.SetFocus;
 end
 else
  CanClose:= True;
end;



This works great for inserting.....However, if I am editing, of course the record always exists, so it makes the dialog stay open and i can't close it.

I need a alternative method for validation  - any Ideas?


IElite
0
IElite
Asked:
IElite
  • 3
1 Solution
 
geobulCommented:
Hi,

You may add a public field to TfrmChild form that will define the action (insert or update):

  TfrmChild = = class(TForm)
    ...
  private
    { Private declarations }
  public
    { Public declarations }
    ActionType: integer;
  end;

When you call that form you define the action first:

  frmChild.ActionType := 1;
  frmChild.Show;

and in the OnCloseQuery you check the action type and take the appropriate tasks:

procedure TfrmChild.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  case ActionType of
    1: begin
         // your code for insert here
         if frmData.tblChildren.Locate('ParentId;Name', VarArrayOf([frmData.tblParents.Fields[0].asInteger,
                             edtName.Text]),[])  then
           begin
             CanClose:= False;
             edtName.SetFocus;
           end;
        end;
    2: begin
         // your code for update here
        end;
    3: begin
         // your code for delete here
        end;
    else
      CanClose:= True;
  end;
end;

Regards, Geo
0
 
geobulCommented:
 // or call the form like
  frmChild.ActionType := 2; // update the current record
  frmChild.Show;

Regards, Geo
0
 
geobulCommented:
I forgot to set CanClose to true in the beginning (else part is no longer necessary in that case). And for updating you actually need to check if the record doesn't exist. If it doesn't exist then you can't modify it:

procedure TfrmChild.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose:= True;
  case ActionType of
     1: begin
           // your code for insert here
           if frmData.tblChildren.Locate('ParentId;Name', VarArrayOf([frmData.tblParents.Fields[0].asInteger,
                             edtName.Text]),[])  then
           begin
             CanClose:= False;
             edtName.SetFocus;
           end;
         end;
     2: begin
           // your code for update here
           if not frmData.tblChildren.Locate('ParentId;Name', VarArrayOf([frmData.tblParents.Fields[0].asInteger,
                             edtName.Text]),[])  then
           begin
             CanClose:= False;
             edtName.SetFocus;
           end;
         end;
  end;
end;

Regards, Geo
0
 
IEliteAuthor Commented:
That Worked Perfect - Thanks!

IELite
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now