Solved

Add values to table in MS Access?

Posted on 2004-08-17
5
697 Views
Last Modified: 2008-01-09
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
Comment
Question by:wavget
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 11

Expert Comment

by:calinutz
ID: 11827603
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
 
LVL 5

Accepted Solution

by:
Darth_helge earned 125 total points
ID: 11827917
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
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 11828562
wavget,
try with Runsql and place your sql statement there :
AccessApplication1.DoCmd.RunSQL('insert into table (field1,field2,...) values (value1,value2,...)',false);
0
 
LVL 1

Author Comment

by:wavget
ID: 11830184
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
 
LVL 5

Expert Comment

by:Darth_helge
ID: 11838413
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Yen format 3 75
SUM 2 INTEGER ARRAYS INTO 1 10 118
Typecasting TBytes to Integer in Delphi XE8 2 68
SQL Server Express automatically execute SQL or SP 8 69
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question