Solved

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

Posted on 2002-04-30
13
253 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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