Solved

Add values to table in MS Access?

Posted on 2004-08-17
5
688 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now