Solved

DBGrid Cells II

Posted on 1997-12-03
9
379 Views
Last Modified: 2010-04-04
I have now a simple question:
      Using que ansWer to my previous question I acess the data in a DBGrid cell and check if this data exists in another table. Now I don't know how to make the DBGRid accept the typed text. I try the Post method to send the data to the table, but I receiva the message "Stack Overflow Error". I thing that when the post is called it calls the OnSetText event againg. How can I send the data to the table after typed?
0
Comment
Question by:oberdan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352819
Have you set the record to edit?
Dbgrid to edit?

0
 

Author Comment

by:oberdan
ID: 1352820
Explaining better:

On the Form OnCreate event I have:

begin
Table1.Open;
Table1.Append;
Table1.Setkey;
end

The first field of the table is called "Number", and is indexed.

On the event TForm1.Table1NumberSetText  I have:

begin
Table1.FieldByName('Number').AsString:=Text;
if Table1.GotoKey then
      begin
       MessageDlg('Number already exists!',mtError,[mbok],o);
{here I'd like to cancel the operation and clear the cell}
      end
else
{Here I'd like to send the data to the table}
      
end;

My problem is the Stack Overflow. When I do any change whit the data the event is called again.


What I want is:
1 - Get the user's text.
2 - Search in Table1 for the text.
3 - If exist, display message and cancel operation.
    Else send the data to table

Do you know any good book that explain how to make this operations? Something like "Mastering Databases in Delphi"?

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352821
Hi oberdan,

Sorry, but this I don't understand! What's the purpose of this code? On the OnCreate you open the table (ok, i can get this) but you also append a record to it (I already find it strange if you append it when you activate it, but when you create a form? That's only at the startup!).

Please, explain us what you want to happen.

But now, for the question : I think it has to do with the fact that when you search the table, you move the pointer away from the record you want to insert (I think, am not sure) So you can try this. Before you search :

var RecordBookmark : TBookmark; (in procedure heading)

RecordBookmark := table1.GetBookMark;

After you have searched and before you cancel the record (table1.Cancel) or post the record (table1.post) you first go back to the record by

table1.GotoBookMark(RecordBookMark);

and offcourse free the bookmark

table1.FreeBookMark(RecordBookMark);

You can also use the try except on ... end if you want to check for existing indexes, because at key violation delphi sends an exception.



 
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:oberdan
ID: 1352822
Hello ZifNab

My Problem is:
      In a DBGrid, to verify if the user's entry already exist in a Table. If exists, Display a message and cancel the entry, else, proceed. I don't know if to make this I really want check an exception. I was trying the OnSetText method of the TField, but it results in a infinite recursion and Stack Overflow.
      Maybe you don't understand me because my english is bad.


                        []'s

                                          
       
0
 

Author Comment

by:oberdan
ID: 1352823
Adjusted points to 60
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1352824
Hi again!

Maybe you should try to create another Table object and
in it you will check if this value already exists,
and not to move from record to record in the editted
table object.
You can also use SQL objects to find if that value was entered.

Good luck !
0
 
LVL 3

Accepted Solution

by:
Matvey earned 60 total points
ID: 1352825
OK,

try this code :
It checks if the value exists and if it does then it cancel
Posting. It's attached to the BeforePost Event.

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
var t : TQuery;
begin
t := TQuery.Create(self);

t.DataBaseName := table1.DatabaseName;
t.SQL.Add('select * from animals where name = ''' +                             table1.Fields[0].asstring + ''';');
t.Open;
if (t.RecordCount > 0) then
Begin
  t.close;
  t.destroy;
  ShowMessage('Value exists!');
  Abort;
  end;
end;

0
 
LVL 3

Expert Comment

by:Matvey
ID: 1352826
( My previous example uses the Animals table in DBDEMOS. )
0
 

Author Comment

by:oberdan
ID: 1352827
The Abort procedure I'd never thing.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question