Hello Community,
I programmed a program which can read a MS Access-database.
But the programm can’t save changes to the database-file.
I have to work with this type of database.
Operating system: Windows 10.
Lazarus IDE v1.6.4.
I did what this tutorial is saying:
https://www.youtube.com/watch?v=LW3wuJAB2hkBecause I don’t want expect you to watch the long video I write what I did here:
1) Install the following driver:
Microsoft Access Database Engine 2010 Redistributable
https://www.microsoft.com/en-my/download/details.aspx?id=132552) Configure the driver. See attachment-picture.
3) Set into the form the following objects:
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ODBCConnection1: TODBCConnection;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
4) Initialize this objects with the following content:
procedure TForm1.init_all;
begin
init_DBGrid;
init_DataSource1;
init_ODBCConnection1;
init_SQLTransaction1;
init_SQLQuery1;
end;
procedure TForm1.init_DBGrid;
begin
DBGrid1.DataSource := DataSource1;
end;
procedure TForm1.init_DataSource1;
begin
DataSource1.DataSet :=SQLQuery1;
end;
procedure TForm1.init_ODBCConnection1;
begin
ODBCConnection1.DatabaseName := 'lib_for_GUI';
ODBCConnection1.UserName := '';
ODBCConnection1.Password := '';
ODBCConnection1.Transaction := SQLTransaction1;;
ODBCConnection1.Connected := true;
end;
procedure TForm1.init_SQLTransaction1;
begin
SQLTransaction1.DataBase := ODBCConnection1;
SQLTransaction1.Active := true;
end;
procedure TForm1.init_SQLQuery1;
begin
SQLQuery1.DataBase := ODBCConnection1;
SQLQuery1.Transaction := SQLTransaction1;
SQLQuery1.SQL.Text := 'select * from Altium';
SQLQuery1.UsePrimaryKeyAsKey := false;
SQLQuery1.Active := true;
end;
After the procedure “Form1.init_all” worked, the content of the database is watched in the DBGrid1 correctly.
I can select the table with other orders for particular columns. It function.
But I can’t save changes to the database-file.
In the following link is described how to save changes into the database:
http://wiki.freepascal.org/SQLdb_Tutorial2The homepage describes to use this following two lines:
SQLQuery1.ApplyUpdates();
SQLTransaction1.Commit;
In the following procedure I manipulate a field and want save the change.
But it doesn’t work. In the attachment you can see the error-message. A part of the error-message is in german. I translated this part into english.
procedure TForm1.Button_Save_to_h00001Click(Sender: TObject);
begin
SQLQuery1.Close;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add('UPDATE Altium SET h_MGR = ''huibuh'' WHERE PartNumer = ''h00001''');
ShowMessage('SQLQuery1.SQL.Text=' + sLineBreak + SQLQuery1.SQL.Text );
ODBCConnection1.connected := True;
SQLTransaction1.Active := True;
SQLQuery1.Open;
SQLQuery1.ApplyUpdates();
SQLTransaction1.Commit;
end;
Please could help me somebody?
I am trying to solve the problem for 3 days.
PS.:
I am wondering at the following:
In the driver i set a username and a password. But in the Delphi-Code i left the username and password empty. And it works.
If I write the correct username and password in the delphi-code, the problem is not solved.