Recent

Author Topic: [Solved] DBF Locate then Edit  (Read 6094 times)

jonek

  • Newbie
  • Posts: 5
[Solved] DBF Locate then Edit
« on: January 12, 2010, 05:28:29 pm »
Hi. I fear I am about to betray my newbie status...

I have a DBF table. I use it to manually populate a TListBox. I would like the user to click on a name in the list and click an edit button, which then throws up an edit box to allow them to change the name, then repost it to the database.

Here's the code at the moment.

Code: [Select]
begin
  if UsersList.ItemIndex=-1 then Exit;
  if not UsersTable.Locate('NAME',UsersList.Items[UsersList.ItemIndex],[]) then
  begin
    MessageDlg('Error 103',mtError,[mbOK],0);
    Exit;
  end;
  EditName:=UsersTableName.Text;
  repeat
    if not InputQuery('Edit user', 'Re-enter the user''s name',EditName) then Exit;
  until EditName<>'';
  UsersTable.Edit;
  UsersTableName.Text:=EditName;
  UsersTable.Post;
  BuildUsersList;
end;

I don't get error 103. The name in the InputQuery is correct. BuildUsersList is my own routine to repopulate the list. But when finished editing and click OK, the last record in the database is the one edited and posted, regardless of the name selected in the list.

I hope that makes sense. Any direction gratefully received.

Jon
« Last Edit: January 20, 2010, 08:27:47 pm by jonek »

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: DBF Locate then Edit
« Reply #1 on: January 12, 2010, 09:46:53 pm »

What is  UsersTableName.Text:=EditName;?

For save a record

Code: [Select]
Dbf1.Edit; 
Dbf1.FieldByName('nume_caj').asinteger:=number;
Dbf1.FieldByName('fec_caja').AsDateTime:=dateVar;
Dbf1.Post;


Regards

jonek

  • Newbie
  • Posts: 5
Re: DBF Locate then Edit
« Reply #2 on: January 12, 2010, 11:22:02 pm »
Thanks for your reply.

It's the same as saying

     UsersTable.FieldByName('Name').AsString:=EditName;

...which I have also tried, for thoroughness. Same result.

The actual posting of the record is fine. It's the fact it's posting the wrong record that is the issue.

jonek

  • Newbie
  • Posts: 5
[Solved] DBF Locate then Edit
« Reply #3 on: January 20, 2010, 08:26:23 pm »
Well, here's what I had to do to make it work.

Code: [Select]
begin
  if UsersList.ItemIndex=-1 then Exit;
  if not UsersTable.Locate('NAME',UsersList.Items[UsersList.ItemIndex],[]) then
  begin
    MessageDlg('Error 103',mtError,[mbOK],0);
    Exit;
  end;
  EditName:=UsersTableName.Text;
  repeat
    if not InputQuery('Edit user', 'Re-enter the user''s name',EditName) then Exit;
  until EditName<>'';
  UsersTable.Locate('NAME',UsersList.Items[UsersList.ItemIndex],[]);
  UsersTable.Edit;
  UsersTableName.Text:=EditName;
  UsersTable.Post;
  BuildUsersList;
end;

I don't really understand why the second Locate line is necessary. However, moving on...

 

TinyPortal © 2005-2018