• Homework
  • Thirds
  • Upper Thirds
  • Fourth
  • Divisions
    • ICT
    • Computer Science
  • Fifth
  • Lower Sixth
  • Upper Sixth
    • Upper Sixth Theory
  • Blog
  • Careers
  • Unity
  • Networking

Delphi and Access walkthrough

Updating the database

Being able to view the data is one thing, but we need to be able to update it.
In this example, using the test database, we'll set the score from a drop down 'Combobox', then write the new score back to the Access database.
  • This form has two DBEdit fields and one regular Combobox (not a DBCombobox)
  • When the score is chosen from the box, the field and database need to be updated.

Note:  Before you try the coding below - make sure that your database will update by making changes in the DBEdit fields and writing to the database by clicking on the green tick in the Navigator. 
Picture
Simply updating the DBEdit.text is not enough.  We need to actually update the field in the query object - and then post that change back to the database itself.  The steps are:
  • Set the query object to edit mode
  • Update the field in the query object
  • use 'post' to commit the changes
procedure TForm3.ComboBox1Change(Sender: TObject);
begin
    FDquery1.Edit;
    FDquery1.fieldbyname('UserScore').value := strtoint(combobox1.Text);
    FDquery1.post;
end;
This should work and the database changes should be visible in access, if you reload the database.  
Powered by Create your own unique website with customizable templates.