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

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

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
chmod101
Asked:
chmod101
  • 3
  • 2
  • 2
  • +3
1 Solution
 
Tomas Helgi JohannssonCommented:
   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
 
chmod101Author Commented:
Well, I tried that, and it is still deleting the next entry in the database. Any other thoughts?
0
 
Tomas Helgi JohannssonCommented:
This should work.
Is the index on the right field ??
What what is the type of [problem] ??

Regards,
Tomas Helgi
0
Independent Software Vendors: 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!

 
kretzschmarCommented:
just as tomas said

try this

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

meikl ;-)

0
 
nestoruaCommented:
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
 
nestoruaCommented:
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
 
CrazyOneCommented:
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
 
chmod101Author Commented:
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
 
kretzschmarCommented:
? what database do you use ?
0
 
CrazyOneCommented:
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
 
YodaMageCommented:
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
 
YodaMageCommented:
Do you have a case sensitivity issue with a case insensitive varchar/string based index?
0
 
chmod101Author Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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