0

I am getting this error with my delphi code below after following this tutorial: Delphi 7: ADO, need basic coding example . I have seen other solutions however I am unsure what they really mean or what I should actually do. I was wondering if someone could see if there was an obvious problem or explain what I should be checking. Error I am getting on the ADOConnection1:=TADOConnection.create(nil) line.

[Microsoft][ODBC MANAGER] data source name not found and default driver not specfied

 unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs,ADODB,Stdctrls, DB;

type
  TForm1 = class(TForm)
     ADOConnection1: TADOConnection;
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
  private
  public
     procedure createdb;
     procedure closedb;
   end;

 var
   Form1: TForm1;

 implementation

{$R *.dfm}
Procedure TForm1.createdb;
var
nameDB:string;
connectionstring:string;
begin
connectionstring:='Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=E:\Project;';
namedb:='Project1db.accdb';
ADOConnection1:=TADOConnection.Create(nil);
ADOConnection1.LoginPrompt:=false;
ADOConnection1.Connected:=true;
ADOConnection1.Execute('CREATE DATABASE IF NOT EXISTS 
Project1db.accdb',cmdtext);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
createdb;
closedb;
end;

 Procedure TForm1.closedb;
 begin
   ADOConnection1.Free;
 end;
 end.
  • We readers cannot see your screen: Which line of your code cause the error? Btw, there is a basic error in your code: Components which you drop on your form - like Button1 and ADOConnection1 are automatically created when your app starts up and freed when it closes down. So, you do not need - and should not include - your `ADOConnection1:=TADOConnection.Create(nil)` – MartynA Feb 26 '18 at 09:46
  • Your `Data Source` parameter just points to not existing data source. Data source is something you can configure in the [ODBC Data Source Administrator](https://support.office.com/en-us/article/Administer-ODBC-data-sources-B19F856B-5B9B-48C9-8B93-07484BFAB5A7). – Victoria Feb 26 '18 at 09:47
  • @Victoria I am not the admin for the computer I am on since it is a school computer. How would you recommend doing it otherwise? Should I be using OLE? – jane wilson Feb 26 '18 at 10:00
  • Try [for example this](https://stackoverflow.com/a/41897116/8041231). – Victoria Feb 26 '18 at 10:08
  • @Victoria: Actually, on my Win10 set-up, the Jet provider doesn't seem to want to open Access2016 .accdb files. In a few minutes I'll post a set of instructions for how to configure the AdoConnection for her example db. – MartynA Feb 26 '18 at 10:13
  • @MartynA, I didn't mean to use a different `Provider` parameter in the connection string rather than `ADOX.Catalog` OLE object. – Victoria Feb 26 '18 at 23:58

1 Answers1

1

Assuming your Project1db.accdb already exists in E:\Project, the way to set up your AdoConnection is as follows:

In the IDE, click your AdoConnection and set its LoginPrompt property to False.

Click the [...] button in the ConnectionString property

In the ConnectionString pop-up, click the Use Connection String radiobutton then click the Build button below it.

On the Provider tab, set the Provider type to

Microsoft.ACE.OLEDB.12.0

On the Connection tab, set the Data Source to

e:\Project\Project1db.accdb

Then click the Test Connection button. You should get a pop-up confirming that the connection succeeded.

If the Project1DB.accdb file doesn't already exist, you could use @Victoria's for example this link to create it or use MS Access to do it. But you will still need something like your existing code to open the database.

As I said in a comment, you shouldn't be calling ADOConnection1 := TAdoConnection.Create(Nil) so delete that line and the one ADOConnection1.Free. I hope you can understand now that you can use the Object Inspector to configure the AdoConnection, etc.

Btw, I'm sorry if my comment on one of your questions yesterday confused you into trying to use ODBC (which is fine if you are the administrator of your machine but can be trickier if you aren't). To use an existing ODBC datasource, you would need to set the AdoConnection's Provider type to something like Microsoft OLE DB Provider for ODBC Drivers, rather than any of the Access-specific providers - ODBC is a generic way of working with various different types of database (including Access). Btw, you can use an ODBC dsn to create an Access database, as an alternative to creating the database using the ADOX objects as per the article @Victoria linked.

MartynA
  • 30,454
  • 4
  • 32
  • 73