Solved

Deleting entry in TTable deletes the next entry, not the current one

Posted on 2002-04-30
13
256 Views
Last Modified: 2010-04-04
Alright, I have a TTable that has a db-table set as its tablename.  The following is the code used to delete an entry from the table:

  table1.FindKey([problem]);
 
  // Cut data out
  table1.Edit;
  table1.Delete;

I have verified that the FindKey is finding the correct record in the table, but it deletes the next recrod in the table.

Any thoughts as to why?

chmod101
0
Comment
Question by:chmod101
[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
  • 3
  • 2
  • 2
  • +3
13 Comments
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 50 total points
ID: 6980948
   Hi!

The reason is that you call table1.Edit which locks the record in Edit mode. Then the table1.Delete deletes the
next unlocked reocord.
So simply remove the table1.Edit line and it should delete the current record.

Regards,
Tomas Helgi
0
 

Author Comment

by:chmod101
ID: 6981006
Well, I tried that, and it is still deleting the next entry in the database. Any other thoughts?
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 6981055
This should work.
Is the index on the right field ??
What what is the type of [problem] ??

Regards,
Tomas Helgi
0
Technology Partners: 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!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6981401
just as tomas said

try this

if table1.FindKey([problem]) then
 // Cut data out
  table1.Delete
else
  showmessage('Record not found');

meikl ;-)

0
 
LVL 4

Expert Comment

by:nestorua
ID: 6981577
HI,
To delete the proper entry you must have your table in
dsBrowse state.
Check if your table in this state and after that delete the current row.
Sincerely,
Nestorua.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 6981578
HI,
To delete the proper entry you must have your table in
dsBrowse state.
Check if your table in this state and after that delete the current row.
Sincerely,
Nestorua.
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6982451
Well I just tested this and I had no problem.

Created a table with 4 fields
Indexed two fields
Set the table componets IndexName to the one I gave in the table.

I used a dbgrid to watch was happening hence the reason for using Sleep so as to test which record the cursor moved to before doing the Delete. It worked for me.

const
  n = 'i';
  l = 'm';

  if Table1.FindKey([n, l]) then begin
    //The following two lines are used just for testing
    //where the cursor is positioned on the grid
    Application.ProcessMessages;
    Sleep(2000);
    Table1.Delete;
  end;

How are you verifying that FindKey has repositioned the cursor to the correct record? Is the value you are passing to the FindKey the same type as the FieldType?


The Crazy One
0
 

Author Comment

by:chmod101
ID: 6983497
Alright,

Answers to questions:

  1.  the variable problem is a string, and the key value of the table that is being searched using the FindKey method is also a string.

  2.  The FindKey method is finding the apporpriate entry, because after running findkey, I have the following line of code to check it:
 
showmessage(TTable(table).fieldByName('problem').asString);

'problem' is the name of the primary key field in the database, and this message is always right. Whatever entry I was trying to delete, it always finds it.

  3.  I am not even using the indexName or indexFieldName properties.  Is that a problem? I don't have any problem searching the database to the correct entry, I dont even have any problem updating the correct entry when I want to update something. I just can't delete the correct one.

Any more thoughts?

chmod101
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6983811
? what database do you use ?
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6983814
Well try using the indexName and see what happens. Seems weird that you can edit the record OK but deleting removes a different record.
0
 
LVL 4

Expert Comment

by:YodaMage
ID: 6984375
Try this :

var
 i : integer;

i := 1;
if table1.findkey([value]) then
begin
  showmessage('passed this code' + inttostr(i) + 'times');
  inc(i);
  if table1.state = dsbrowse then
  begin
    Showmessage(table1.fieldbyname('name').asstring);
    table1.delete;
    showmessage('deleted a record' + inttostr(i) + 'time');
  end
  else
    showmessage('Screwed up code, table in wrong state');
end
else
  showmessage('No Record');


I'm very curious to see the results from above as I think it will point out your issue directly.
0
 
LVL 4

Expert Comment

by:YodaMage
ID: 6984379
Do you have a case sensitivity issue with a case insensitive varchar/string based index?
0
 

Author Comment

by:chmod101
ID: 6991333
Alright, I have solved the problem. What I thought was a problem with my understanding of deleting a record  was actually a problem with my own program.  You see, I had added the following feature to my program: The user has the option of allowing the database to be updated whenever a person changes from viewing one entry to the next.  The default for this option is on.  Therefore, this is what was happening:

   1.  The correct entry was being loaded into a data structure and edited by the user.
   2.  The delete option was selected, and the correct entry was being deleted.  however, the data in the data structure was not being cleaned out.
   3.  There is a treeview being used to navigate the database table, and after a deletion, it is emptied and then repopulated with the new database data.  When this happens, the onChange event is triggered, and the program updates the currently-selected database entry in the database with the information in the data structure.

  Basically, the correct entry was being deleted, and then the next entry was being overwritten with the just-deleted entry's data, thus making it appear that the next entry was being deleted.


I hope that explains it.  Since the answer that Tomas Helgi supplied was correct for what the problem appeared to be, I am giving him the points for this question.

Thank you all for your help.
chmod101
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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