Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 700
  • Last Modified:

Add values to table in MS Access?

I need to add a few values to a table in a Microsoft Access data base. Using a TAccessApplication component I can open the database and the table with the following code:

  AccessApplication1.Connect;
  AccessApplication1.Visible := True;
  AccessApplication1.OpenCurrentDatabase(C:\MyDatabase.mdb', True);
  AccessApplication1.DoCmd.OpenTable('TableName', acNormal, acAdd);

It opens the table and I can manually add values... how do I add a record in code?

Thanks in advance!

D2.
0
wavget
Asked:
wavget
1 Solution
 
calinutzCommented:
using ADO query component
...
AdoQuery1.Active:=false;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('insert into table (field1,field2,...) values (value1,value2,...)');
AdoQuery1.ExecSQL;
...
This is how you insert a record into Access table

Also you can update (modify) a record using the same syntax but with different SQL string:
...
AdoQuery1.Active:=false;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('update table set field1=value1, field2=value2,... where fieldx=valuex');
AdoQuery1.ExecSQL;
...

0
 
Darth_helgeCommented:
here's a quick example:
First, make an ODBC to your database.

procedure TForm1.Button1Click(Sender: TObject);
var
AdoQuery1: TAdoQuery;
Con: TAdoConnection;
begin
Con := TAdoConnection.create(nil);
Con.ConnectionString := Provider='MSDASQL.1;Persist Security Info=True;Data Source=YOUR ODBC;Initial Catalog=YOUR CATALOG';
Con.Active := true;

AdoQuery1 := TAdoQuery.Create(nil);
AdoQuery1.Connection = Con;

AdoQuery1.SQL.Text('insert into table (datefield,integerfield,stringfield) values (' + FloatToStr(MyDate)+',' + IntToStr(MyInteger) +',''' + MyString + ''')';
AdoQuery1.ExecSQL;

end;

I always treat Datevariables like a Float Value. It's much easier. When you are inserting a string, notice that you need some extra quotes.
The full sql sentence passed to your database will be like this:
insert into table (datefield,integerfield,stringfield) values ( 34545.4543 , 42 , 'This is my string' )


0
 
Amir AzhdariCommented:
wavget,
try with Runsql and place your sql statement there :
AccessApplication1.DoCmd.RunSQL('insert into table (field1,field2,...) values (value1,value2,...)',false);
0
 
wavgetAuthor Commented:
Thanks Darth_helge for your answer. It wasn't immediately obvious to me that I was supposed to change ‘table’ into the name of the table, and that ‘datefield, integerfield, stringfield’ meant the names of the fields, but after some searching for running SQL commands I figured that out. Thanks for the hint about the date variable too.
For future generations, this is what I ended up wih:

AccessApplication1.DoCmd.RunSQL('INSERT INTO TableName (T1,T2,T3) Values (25,125,-20)',false);
Where T1, T2, and T3 are field names, and 25, 125, and -20 is the data inserted into those fields.

D2.
0
 
Darth_helgeCommented:
I was a little fast with my datevariable...
FloatToStr works as a Datetime.
When you are working with only dates with no time (like 2004-08-07) the easiest way to insert this is by truncating the variable so it becomes an integer.
Like this:
AdoQuery1.SQL.Text('insert into table (datefield,integerfield,stringfield) values (' + IntToStr(trunc(MyDate))+',' + IntToStr(MyInteger) +',''' + MyString + ''')';
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now