Solved

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

Posted on 2002-04-30
13
250 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 24

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 24

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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

19 Experts available now in Live!

Get 1:1 Help Now