?
Solved

Delphi ADO connection to MS Access DB

Posted on 2011-05-08
9
Medium Priority
?
638 Views
Last Modified: 2012-05-11
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
Comment
Question by:error77
  • 4
  • 3
  • 2
9 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35716916
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
 
LVL 25

Expert Comment

by:epasquier
ID: 35716924
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
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 35716925
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
Independent Software Vendors: 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!

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35716930

Welcome back epasquier
The Delphi Zone is not the same without you
0
 
LVL 25

Expert Comment

by:epasquier
ID: 35717088
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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35717115

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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35718879
lol, congrats epasquier...
it does get busy with a few kids doesn't it ... my wife wants a 4th ... :)


0
 
LVL 25

Expert Comment

by:epasquier
ID: 35719036
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 35719716
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 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