views:

206

answers:

2

hi all I want to write an app that is an address book and uses a access .mdb file. I know how to insert a new contact into the database, but i do't know how to edit a contact. I put my test project in here AddressBookTest.zip. Any help is welcomed

A: 

It looks like you're using SQL commands instead of Edit/Insert/Post. Therefore what you need is the UPDATE command:

UPDATE tblContacts
SET FirstName = '''+frmEd.edFirstName.Text+''''
WHERE ID=10

To add more than one value, separate with commas, eg.

SET FirstName = 'John', LastName = 'Smith'
_J_
+1  A: 

Your code for INSERT must do This:

      qryAux.SQL.Text:='INSERT INTO tblContacts(FirstName, LastName) VALUES( ' +
        QuotedStr(frmEd.edFirstName.Text) + ',' +
        QuotedStr(frmEd.edLastName.Text) + ')';

Here is correct for insert the two values.

In Edit mode, for retrieve values from DB, you must execute the Open, not the ExecSQL. And after do the Open, you must test if the record is finded. See this code:

   qryAux.Open;

   // Finded?
   if not (qryAux.Eof) then begin
     frmEd.edFirstName.Text:=qryAux.FieldByName('FirstName').AsString;
     frmEd.edLastName.Text:=qryAux.FieldByName('LastName').AsString;
   end;

Regards.


Neftalí -Germán Estévez-

Neftalí
NOTE:Also, you can use parameters in the aryAux Query; Using paramaters it's not needed use QuotedStr.-> qryAux.SQL.Text:='INSERT INTO tblContacts(FirstName, LastName) VALUES(:fist, :last)';
Neftalí
You do not need to supply the field names in an INSERT statement, as long as you are supplying values for all of the fields, in order.
_J_
this is what i wanted, it works fine
Remus Rigo