• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 894
  • Last Modified:

Delphi ADO connection to MS Access DB

Hi all,

I need some sample code on how to connect via ADO to an MS Access database with is on the same directory as my application.

Can anyone help please?

THanks
0
error77
Asked:
error77
  • 4
  • 3
  • 2
1 Solution
 
Ephraim WangoyaCommented:
Here
Assume your database is called mydata.mdb
function TForm1.InitConnection: Boolean;
var
  ConnString, DatabaseName: string;
begin
  DatabaseName := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'MyData.mdb';
  ConnString := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False', [DatabaseName]);
  ADOConnection1.ConnectionString := ConnString;
  ADOConnection1.Connected := True;
  Result := ADOConnection1.Connected;
end;

Open in new window

0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
There is only too much examples out there to list all of them.
look there, a quick selection of what Google can bring

http://www.databasedev.co.uk/microsoft-access-and-borland-delphi.html

http://www.devlist.com/ConnectionStringsPage.aspx

http://www.latiumsoftware.com/en/delphi/00036.php
0
 
Ephraim WangoyaCommented:
If you want to pass the database name to the function, you can do this
function TForm1.InitConnection(const ADBName: string): Boolean;
var
  ConnString, DatabaseName: string;
begin
  DatabaseName := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + ADBName;
  ConnString := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False', [DatabaseName]);
  ADOConnection1.ConnectionString := ConnString;
  ADOConnection1.LoginPrompt := False;
  ADOConnection1.Connected := True;
  Result := ADOConnection1.Connected;
end;

procedure TForm1.btnConnectClick(Sender: TObject);
begin
  InitConnection('MyData.mdb');
end;

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Ephraim WangoyaCommented:

Welcome back epasquier
The Delphi Zone is not the same without you
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
Thank you ewangoya !

I've had to reorganize my priorities lately. You can imagine : I'm a father for the second time since yesterday (little boy, after a girl of almost 3 yr). I'm alone tonight only because the mother is still at hospital, so I indulged myself a little visit here. Besides, last time I checked, you seemed to be handling very well, and it was almost useless for me to stay awake until you couldn't answer a question. And you're fast too.

I've been impressed, I thought I was (once) flexible enough to handle questions at about any time, but you, are clearly a non-stop expert. I couldn't even find out in which Time Zone you live. When do you sleep exactly ?
0
 
Ephraim WangoyaCommented:

Congratulations
Thats great, you are going to be even more busy for a while.
I myself have an eight and a three year old, both boys and I know what joy the little ones bring. I just sent the 3yr old to school this year (I'm not exactly sure what 3 year old kids do in school)

Congratulations once again man.

Oh, and I do get a little sleep once the kids stop jumping and screaming all over the place
0
 
Geert GOracle dbaCommented:
lol, congrats epasquier...
it does get busy with a few kids doesn't it ... my wife wants a 4th ... :)


0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
Geert : if your brain is not fried yet with 3 kids screaming and running all over the place, say NO thank you !
Or maybe it is just too late already and a 4th would not change the fact that you are totally screwed ;o)

For me, the choice is easy : a 3rd would mean change the car, which can barely hold in the garage anyway, so change the flat as well, which is short of one room already (not a problem when they are small, but we know already that we won't be able to stay there a lot more than 7 years). And we can't afford that. So 2 is great, especially a girl and a boy. Maybe in 7 years, we'll want a third, but that is unlikely.

@error77 : I don't know if you are still monitoring this thread, if that is the case, sorry it has become a chatroom for guys getting too old
0
 
Geert GOracle dbaCommented:
No point in talking about my brain ... it's been replaced some decades ago

lol 3 kids screaming ... my wife runs a nursery at home
she's had 11 screaming in the back at most so far.
feeding at lunchtime is like multitasking ... only women can do that :)

i tried explaining her oracle databases have parallel queries now ...
but then she just looks like she doesn't have a clue what i'm talking about :)

i don't have a problem with the car ... it can hold 7 (vw caddy maxi)
playing golf forced me into getting a caddy :)

the only problem with more kids, is keeping them away from each other when fighting
otherwise they keep each other occupied
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now