| View previous topic :: View next topic |
| Author |
Message |
sara cox Guest
|
Posted: Sat Dec 27, 2003 5:54 am Post subject: How do I add a record to an MS Acess DB using Delphi and ADO |
|
|
Hi!
I am a newbie and I would like to add records to an MS Access DB using
Delphi and ADO within a DLL I wrote in Delphi 7.
I have a DSN connection. I (obviously) don't want to use a form - the
DLL is accessed through C# / .NET and it sends a string of data which
needs to be committed to the Access DB.
Right now the DLL is working and I can pass a string to it and it pops
up with a message box with the string.
Do I use a unit in delphi? A function? A procedure?
If you can supply me with some simple sample code, I would greatly
appreciate it. Thanks in advance!
Sara
xoxo
|
|
| Back to top |
|
 |
MikeB Guest
|
Posted: Sat Dec 27, 2003 5:51 pm Post subject: Re: How do I add a record to an MS Acess DB using Delphi and |
|
|
DSN-Less connection. DBQ can be passed or get from INI file. Fields collection accesed by ordinal
number or Name.
uses ADODB_TLB;
procedure TForm1.Button1Click(Sender: TObject);
var
RS, CN: OleVariant;
sSql, sCnStr : string;
i : Integer;
begin
sSql := 'SELECT * from Shippers';
sCnStr := 'Provider=MSDASQL.1;Driver={Microsoft Access Driver
(*.mdb)};Dbq=f:AccessSamplesNWind2K.mdb;Uid=admin;Pwd=';
CN := CreateOleObject('ADODB.Connection');
CN.Open(sCnStr);
RS := CreateOleObject('ADODB.Recordset');
RS.ActiveConnection := CN;
RS.CursorLocation := adUseClient;
RS.CursorType := adOpenKeyset;
RS.LockType := adLockOptimistic;
RS.Open(sSql);
RS.Addnew;
RS.Fields['CompanyName'].value := 'My New Shipper';
RS.Fields['Phone'].value := '(555)555-1212';
//RS.Fields[1].value := 'My New Shipper';
//RS.Fields[2].value := '(555)555-1212';
RS.MoveFirst;
RS.Update;
RS.Close;
end;
"sara cox" <saracox2004 (AT) yahoo (DOT) com> wrote
| Quote: | Hi!
I am a newbie and I would like to add records to an MS Access DB using
Delphi and ADO within a DLL I wrote in Delphi 7.
I have a DSN connection. I (obviously) don't want to use a form - the
DLL is accessed through C# / .NET and it sends a string of data which
needs to be committed to the Access DB.
Right now the DLL is working and I can pass a string to it and it pops
up with a message box with the string.
Do I use a unit in delphi? A function? A procedure?
If you can supply me with some simple sample code, I would greatly
appreciate it. Thanks in advance!
Sara
xoxo
|
|
|
| Back to top |
|
 |
sara cox Guest
|
Posted: Sun Dec 28, 2003 3:45 pm Post subject: Re: How do I add a record to an MS Acess DB using Delphi and |
|
|
MikeB! You are my hero! Thanks for the help. I'm going to give it a
shot today and let you know if it worked.
Thanks SOOOO much!
Sara
xoxo
"MikeB" <m.byerleyATVerizonDottieNettie> wrote
| Quote: | DSN-Less connection. DBQ can be passed or get from INI file. Fields collection accesed by ordinal
number or Name.
uses ADODB_TLB;
procedure TForm1.Button1Click(Sender: TObject);
var
RS, CN: OleVariant;
sSql, sCnStr : string;
i : Integer;
begin
sSql := 'SELECT * from Shippers';
sCnStr := 'Provider=MSDASQL.1;Driver={Microsoft Access Driver
(*.mdb)};Dbq=f:AccessSamplesNWind2K.mdb;Uid=admin;Pwd=';
CN := CreateOleObject('ADODB.Connection');
CN.Open(sCnStr);
RS := CreateOleObject('ADODB.Recordset');
RS.ActiveConnection := CN;
RS.CursorLocation := adUseClient;
RS.CursorType := adOpenKeyset;
RS.LockType := adLockOptimistic;
RS.Open(sSql);
RS.Addnew;
RS.Fields['CompanyName'].value := 'My New Shipper';
RS.Fields['Phone'].value := '(555)555-1212';
//RS.Fields[1].value := 'My New Shipper';
//RS.Fields[2].value := '(555)555-1212';
RS.MoveFirst;
RS.Update;
RS.Close;
end;
"sara cox" <saracox2004 (AT) yahoo (DOT) com> wrote in message
news:e6c9da04.0312262154.972f3f7 (AT) posting (DOT) google.com...
Hi!
I am a newbie and I would like to add records to an MS Access DB using
Delphi and ADO within a DLL I wrote in Delphi 7.
I have a DSN connection. I (obviously) don't want to use a form - the
DLL is accessed through C# / .NET and it sends a string of data which
needs to be committed to the Access DB.
Right now the DLL is working and I can pass a string to it and it pops
up with a message box with the string.
Do I use a unit in delphi? A function? A procedure?
If you can supply me with some simple sample code, I would greatly
appreciate it. Thanks in advance!
Sara
xoxo
|
|
|
| Back to top |
|
 |
MikeB Guest
|
Posted: Sun Dec 28, 2003 6:41 pm Post subject: Re: How do I add a record to an MS Acess DB using Delphi and |
|
|
"sara cox" <saracox2004 (AT) yahoo (DOT) com> wrote
| Quote: | MikeB! You are my hero! Thanks for the help. I'm going to give it a
shot today and let you know if it worked.
Thanks SOOOO much!
Sara
xoxo
"MikeB" <m.byerleyATVerizonDottieNettie> wrote
DSN-Less connection. DBQ can be passed or get from INI file. Fields collection accesed by
ordinal
number or Name.
uses ADODB_TLB;
procedure TForm1.Button1Click(Sender: TObject);
var
RS, CN: OleVariant;
sSql, sCnStr : string;
i : Integer;
begin
sSql := 'SELECT * from Shippers';
sCnStr := 'Provider=MSDASQL.1;Driver={Microsoft Access Driver
(*.mdb)};Dbq=f:AccessSamplesNWind2K.mdb;Uid=admin;Pwd=';
CN := CreateOleObject('ADODB.Connection');
CN.Open(sCnStr);
RS := CreateOleObject('ADODB.Recordset');
RS.ActiveConnection := CN;
RS.CursorLocation := adUseClient;
RS.CursorType := adOpenKeyset;
RS.LockType := adLockOptimistic;
RS.Open(sSql);
RS.Addnew;
RS.Fields['CompanyName'].value := 'My New Shipper';
RS.Fields['Phone'].value := '(555)555-1212';
//RS.Fields[1].value := 'My New Shipper';
//RS.Fields[2].value := '(555)555-1212';
RS.MoveFirst;
RS.Update;
RS.Close;
|
// Need to add Close to the Connection object here since it was opened in this scope
CN.Close;
| Quote: | end;
"sara cox" <saracox2004 (AT) yahoo (DOT) com> wrote in message
news:e6c9da04.0312262154.972f3f7 (AT) posting (DOT) google.com...
Hi!
I am a newbie and I would like to add records to an MS Access DB using
Delphi and ADO within a DLL I wrote in Delphi 7.
I have a DSN connection. I (obviously) don't want to use a form - the
DLL is accessed through C# / .NET and it sends a string of data which
needs to be committed to the Access DB.
Right now the DLL is working and I can pass a string to it and it pops
up with a message box with the string.
Do I use a unit in delphi? A function? A procedure?
If you can supply me with some simple sample code, I would greatly
appreciate it. Thanks in advance!
Sara
xoxo
|
|
|
| Back to top |
|
 |
sara cox Guest
|
Posted: Wed Jan 07, 2004 8:14 am Post subject: Re: How do I add a record to an MS Acess DB using Delphi and |
|
|
Thanks again MikeB! It totally worked!
To others who are trying this,
you may wish to use a DSN connection by using the following line:
sCnStr := 'Provider=MSDASQL.1;Persist Security Info=False;Data
Source=myDSN';
(myDSN is the name of the DSN you set up in the ODBC connections in
the control panel)
instead of the DSNless version.
Also, you need to go to project / import type library / microsoft
activex data objects 2.0 in order to not get an error when trying to
use ADODB_TLB.
Sara
|
|
| Back to top |
|
 |
MikeB Guest
|
Posted: Wed Jan 07, 2004 2:57 pm Post subject: Re: How do I add a record to an MS Acess DB using Delphi and |
|
|
"sara cox" <saracox2004 (AT) yahoo (DOT) com> wrote
| Quote: | Thanks again MikeB! It totally worked!
To others who are trying this,
you may wish to use a DSN connection by using the following line:
sCnStr := 'Provider=MSDASQL.1;Persist Security Info=False;Data
Source=myDSN';
(myDSN is the name of the DSN you set up in the ODBC connections in
the control panel)
instead of the DSNless version.
|
The real purpose of a DSN-Less connection is it omits the requirement to set up a dsn on EVERY
machine that runs the program. You can furnish the information needed for the connection string
used by the program in an extraneous source like an INI file on the local machine. This is not so
much an issue if the program is run on a stand alone machine.
| Quote: |
Also, you need to go to project / import type library / microsoft
activex data objects 2.0 in order to not get an error when trying to
use ADODB_TLB.
Sara
|
|
|
| Back to top |
|
 |
|