Solved

Programming

Posted on 2002-04-09
33
188 Views
Last Modified: 2010-04-04
i am programming with paradox as backend .i have a programming task to solve. it is master detail relationship .(country and it's states).i would like to check before posting wheather the state is being duplicated . i would like suggestions about what will be the kind of controls that will be best suited for this situation .and how can i check wheather the data is duplicated before posting each record with an easy edit/modify facility
0
Comment
Question by:jonathan2002
  • 14
  • 10
  • 9
33 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6928035
listening . . .
(too low points, too many questions)
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6930380
Hi Jonathon,

The simplest way of doing this is to create that table with primary keys on the state.  That way you are assured that you won't get duplicates (the BDE will raise an exception which you can trap and beautify).

Stu
0
 

Author Comment

by:jonathan2002
ID: 6930526
presently i am using string grid can we edit string grid cell and move through it with tab
plz comment
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6930528
why not use a tdbgrid?
(its designed for database-editing)
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6930543
On what?  Sorry, I'm not following you.

If you create a table with an index on the fields you want to restrict, then you can't add an item to a column which already exists.  For instance, if you have a table that contains fields: country, state and capital, you can create your table with a index on state which means if you try to enter the same state twice, it will raise an error when you attempt to post the data.  This error can be trapped and you can refocus the correct cell in the grid and make them reenter the data.

The other way of doing it would be to add some code to the BeforePost effect on your TTable component and check the new data is unique.

I mentioned before about a primary key.  I appologise for that, because I actually ment an index, I just wasn't thinking.  You can create a secondary index on your paradox table in the Database Desktop.  Open the table you wish to add the index to, click the Restructure button, then, from the Table Properties combo, select Secondary Indexes.  Click the Define button and add the field you wish to become the index.  Highlight the field in the listbox and then check the Unique checkbox.  Save the index with the same name as your table.  Then test it out.  You can also create index at runtime if you wish.

No matter which way you chose to do this, it will require a bit of coding on your behalf.  You won't be able to do it completely automatically.

Stu
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6930585
I thought it said DBGrid!  I didn't see that.  That adds a whole new perspective.  I'd definately go down the path that Meikl suggested (DBGrid), but you will still need an index to assure uniqueness.

Otherwise, you'll need to put some code in behind the grid to check that the data inputted was valid.  The OnSetEditText event is triggered after the user has entered data into a cell.  I'd do something like this:

procedure Form1.GridSetEditText(Sender: TObject; ACol, ARow: Integer;
  const Value: String);

var
  Data: String;
  Count: Integer;

begin
  if ACol <> 2 then {change this to be your column}
    Exit;
  Data := UpperCase(Grid.Cells[ACol, ARow]);
  for Count := Pred(Grid.FixedCols) to Pred(Grid.RowCount) do
    if UpperCase(Grid.Cells[ACol, Count]) = Data then
      begin
        ShowMessage('You can not use this value, its already been used');
        Grid.Cells[ACol, ARow] := '';
      end;
end;

This may need a bit of tweeking because I've done this from memory.

Stu
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6930587
...left one line of code out.  Add a BREAK after the Grid.Cells[ACol, ARow] := ''; line.  That way it won't waste time iterating through the rest of the rows.


Stu
0
 

Author Comment

by:jonathan2002
ID: 6930939
the problem with dbgrid and the table component is that
it writes directly to the database and  i may need to cancel the master and entire detail records.
any body has any suggestions .
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6930944
Yeah, the suggestion I posted above!
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6930956
yep, as stuart already mentioned.

also the tdataset has an onbeforepost-event,
where you can check for duplicates with another dataset-instance, if you will work endexless or if you want to avoid a db-exception (if indexes defined)

meikl ;-)
0
 

Author Comment

by:jonathan2002
ID: 6967902
Mr.kretzschmar
How to create the instance of that dataset so that i can traverse through it and find for duplicates .
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6967935
Hay Jonathon.  Read this: http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20287445

I've given up on this thread. I spent a lot of time explaining stuff and trying to help you out and you haven't bothered to give me any indication at all that what I've said is useful or not.  That's just rude.  Meikl and I gave you an answer and you've just ingored it, so I'm out a here.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6967951
something from my paq,
looks up, if a unique-field already exists,
if yes then a update of other fields is done,
and the original insert will canceled and aborted


procedure TForm1.Table1BeforePost(DataSet: TDataSet);
var i : Integer;
begin
 if table1.state = dsInsert then  //do only if in insert-mode
 begin
   Table2.Open;  //must not be set if table2 already open
   if table2.Locate('CustID',Table1.FieldByName('CustID').Value,[]) then
   begin  //if the Unique ID in the DB
     Table2.Edit;  // Prepare for Update
     for i := 0 to Table1.FieldCount - 1 do //Copy Fields
       table2.Fields[i].Assign(Table1.Fields[i]);
     Table2.Post;  // Save the Update
     Table2.Close; //must not be set if table2 already open
     Table1.Cancel; //Cancel the Insert
     Abort;         //Abort the Post
   end
   else table2.Close; //must not be set if table2 already open
 end;
end;

just to say, what stuart means,
be close in time with your question,
don't let weeks go until you ask for further details,
grade your questions

meikl ;-)
0
 

Author Comment

by:jonathan2002
ID: 6967958
hi Stuart Johnson

i have found that the SetEditText(Sender: TObject; ACol, ARow: Integer;const Value: String); is a protected method
so how am i supposed to get the Method.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6967961
Meikl,

What I meant was that I am pissed off that I've written so much stuff and I haven't had a useful comment back about any of the code I've written.  Sure, the time frame in getting back to us sucks, but I can live with that.

Stu
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6967966
Why couldn't you have said that instead of just ignoring my comments?  And it's not protected in a TStringGrid at all.  It's an public method available in the object inspector.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6967970
>What I meant was that I am pissed off that I've written
>so much stuff and I haven't had a useful comment
>back about any of the code I've written

i understand, such do annoying me too, stuart
0
 

Author Comment

by:jonathan2002
ID: 6967992
hi Stuart Johnson

i have found that the SetEditText(Sender: TObject; ACol, ARow: Integer;const Value: String); is a protected method
so how am i supposed to get the Method.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6968007
TStringGrid events

TStringGrid     Alphabetically     Legend

Derived from TDrawGrid

     OnColumnMoved
     OnDrawCell
     OnGetEditMask
     OnGetEditText
     OnRowMoved
     OnSelectCell
>>>>>OnSetEditText<<<<<
     OnTopLeftChanged

You said "presently i am using string grid can we edit string grid cell and move through it with tab
plz comment"

The comment I posted afterwards was regarding the TStringGrid which has the OnSetEditText method.
0
 

Author Comment

by:jonathan2002
ID: 6968020
i am really sorry Mr. Stuart Johnson and Mr  kretzschmar
i really couldn't look from u r point of view as things in my side was not working well. i know that it's not the way one  should behave when he is one among the group.
i will never repeat this in future
i am really thankfull for the correction from u r side
jonathan

I Haven't Gone through u r code Mr  kretzschmar so i am
not able to comment on that .

Mr. Stuart Johnson reallly sorry for the whole thing. i was trying how to handle and i was switching between TstringGrid/TDBGrid And in the Meanwhile I was forced to do a hell Of other works also.
Kindly Accept this as apologize
jonathan
0
 

Author Comment

by:jonathan2002
ID: 6968031
i am really sorry Mr. Stuart Johnson and Mr  kretzschmar
i really couldn't look from u r point of view as things in my side was not working well. i know that it's not the way one  should behave when he is one among the group.
i will never repeat this in future
i am really thankfull for the correction from u r side
jonathan

I Haven't Gone through u r code Mr  kretzschmar so i am
not able to comment on that .

Mr. Stuart Johnson reallly sorry for the whole thing. i was trying how to handle and i was switching between TstringGrid/TDBGrid And in the Meanwhile I was forced to do a hell Of other works also.
Kindly Accept this as apologize
jonathan
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6968081
Jonathon,

That's cool.  It's just I've started to get really disillusioned with Experts Exchange lately and I really hate taking so much time out to write stuff up and then have to constantly follow it up.  I understand you were having problems - it's just unfortunate that you didn't let us know that you were having those problems because chances are, your app would be up and working properly now.

Now, can you let us know where you're upto.  Have you decided to go down the path of the DBGrid, or are you going to stick with the StringGrid?  The DBGrid is definately the easiest way.  The StringGrid is effective, but does require a fair bit of logic code to ensure things don't go wrong.

Check out Meikl's code.  See if it meets your requirements.  Let us know either way so that we can keep helping you out.  If you're satisfied with his solution, that's excellent.  If you're not, then we can both keep nutting it out until you're going 100%

I am sorry for losing my temper, but I hope you understand why.  I've been with EE for almost 5 years now, and things have changed a lot in those years.  I feel that there are a lot more "information leechers" - people who show up, ask a qusetion and then never grade the question or leave comments.  Then they never show up again.  This sort of thing didn't happen that offen before.

Cheers,

Stu.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6968092
yep
0
 

Author Comment

by:jonathan2002
ID: 6968115
Mr  kretzschmar  I Have Gone through U r Code .And i don't understand which is this table2.and what is it relation with table1 .if this has to point to the same table in the database then why r u copying  the fields from table1 to table2 and then saving the update through it.

My Situation Is country and places in table .when i insert a  new country i have to check wheather the country code exist .if it doesn't exist i can insert places with check for duplication in places .The critical situation is when the user cancels the entire thing .so i am trying to find a solution where the posting and deleting can be avoided
in the database if the user cancels

ignore this if i am running through a useless way of thinking
jonathan
0
 

Author Comment

by:jonathan2002
ID: 6968128
Mr  kretzschmar  I Have Gone through U r Code .And i don't understand which is this table2.and what is it relation with table1 .if this has to point to the same table in the database then why r u copying  the fields from table1 to table2 and then saving the update through it.

My Situation Is country and places in table .when i insert a  new country i have to check wheather the country code exist .if it doesn't exist i can insert places with check for duplication in places .The critical situation is when the user cancels the entire thing .so i am trying to find a solution where the posting and deleting can be avoided
in the database if the user cancels

ignore this if i am running through a useless way of thinking
jonathan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6968151
i explain a bit

table2 is just a copy of table1
(a second object-instance,
not a physical copy of the
table in the database)

table2 tries to locate by given
unique-field(s) the record

if table2 fails to locate,
-table1 does its insert normally

if table2 can locate a record, then the record, which will be inserted by table1 already exists. in this case,
-all modifications of other fields are updated through table2 (instead of this you could also display a message like "record already exists")
-the insert of table1 will be canceled
-the beforepost-event will be aborted
-no record is inserted

just ask, if you are unsure about my explaination

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6968155
>if the user cancels

no changes will be done to the database
0
 

Author Comment

by:jonathan2002
ID: 6968186
Mr kretzschmar how can we create the instance of the table component .
0
 

Author Comment

by:jonathan2002
ID: 6968191
Mr kretzschmar how can we create the instance of the table component with data in it .
0
 

Author Comment

by:jonathan2002
ID: 6968217
Mr kretzschmar how can we create the instance of the table component with data in it .
0
 

Author Comment

by:jonathan2002
ID: 6968221
Mr kretzschmar how can we create the instance of the table component with data in it .
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 6968224
ok, an on the fly

function isDuplicateEntry(ATable : TTable) : Boolean;
var
  t : TTable;
begin
  t := ttable.create(self); //create a second instance at runtime
  try
    t.databasename := ATable.databasename;
    t.tablename := ATable.tablename;
    t.open;
    result := t.locate('CountryID;State',[ATable.FieldByName('CountryID').Value,ATable.FieldByName('State').Value],[]);
  finally
    t.free;
end;

the beforepost event could now look like

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
if table1.state = dsInsert then  //do only in insert-mode
begin
  If isDuplicateEntry(table1) then
  begin
    showmessage('Record already exists');
    Table1.Cancel; //Cancel the Insert
    Abort;         //Abort the Post
  end;
end;

well not sure about the syntax in the locate-method,
(no delphi on hand yet)

meikl ;-)
0
 

Author Comment

by:jonathan2002
ID: 6970493
Thanks for all the effort.thanks to stu also for his effort
and i had finally sticked on to DBGrid as a solution
jonathan
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now