Solved

Change the path to the connection of my database.

Posted on 2009-07-09
17
184 Views
Last Modified: 2012-05-07
Dear Experts,

I have a programm that has a connection to a Access-database.
I have used the ADOConnection1 and the ADOQuery1-components.
Everything works fine.

This what I have as connection string property:
provider=microsoft.jet.OLEDB.4.0;Data Source=C:\Users\Brandon\Documents\MyProject\test,mdb;Persist Security info=false

and I want it change to this:

provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='+IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName))+'Database.pkd;';

But where do i have to put it?

Greetings, Peter Kiers
0
Comment
Question by:peterkiers
  • 5
  • 5
  • 4
  • +1
17 Comments
 
LVL 7

Assisted Solution

by:wrwilson
wrwilson earned 100 total points
ID: 24815950
It would go in the ConnectionString property of your ADOConnection1 object.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24815999
i did or it doesn't work or the syntax is not right.

provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='+IncludeTrailingPathDelimiter(ExtractFilePath(ITKnowLedgeBase))+'test.mdb

is this line correct?
0
 
LVL 13

Assisted Solution

by:rfwoolf
rfwoolf earned 100 total points
ID: 24816006
Assuming your ADOConnection1 is on a DataModule, you would go to your DataModule's oncreate event and say
ADOConnection1.close;
ADOConnection1.ConnectionString := 'provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='+IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName))+'Database.pkd;';
ADOConnection1.Open;

When you do a final build of your application, make sure the ADOConnection1 is not connected because as you know this gets set now at runtime
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 24816017
"is this line correct?"

Normally I do just fine without "includingtrailingpathdelimiter"
So try this:


ADOConnection1 := 'provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=' + ExtractFilePath(Application.ExeName) + 'test.mdb';
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 24816045
I left out "ConnectionString".
Now I forget whether you need to put the \ slash in it or not.
Do yourself a favour and debug this one yourself (because it's fun).
Put a showmessage:

ADOConnection1.ConnectionString := 'provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=' + ExtractFilePath(Application.ExeName) + 'test.mdb';
Showmessage(ADOCinnection1.ConnectionString);

If it shows C:\Users\Brandon\Documents\MyProjecttest.mdb
then you know you need to add a '\' to 'test.mdb;
0
 
LVL 6

Expert Comment

by:FactorB
ID: 24816078
For example on form create, note that you can not change connection string on AdoConnection1 that is already connected, so you need to disconnect, change and reconnect this way, then later you can execute queries or open tables that are associated with AdoConnection1 (or use the name of your connection)

procedure TForm1.FormCreate(Sender: TObject);
begin
ADOConnection1.Connected:=False;
ADOConnection1.ConnectionString:='provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='+IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName))+'Database.pkd;';
ADOConnection1.Connected:=True;
end;

Regards,
B.
0
 
LVL 6

Expert Comment

by:FactorB
ID: 24816117
Ups, again something similar was posted during the time I was typing, I should take touch typing classes :)
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24816219
I have this:

procedure TForm1.FormCreate(Sender: TObject);
begin
ADOConnection1.Connected:=False;
ADOConnection1.ConnectionString:='provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='+IncludeTrailingPathDelimiter(ITKnowledgeBase.exe))+'test.mdb;';
ADOConnection1.Connected:=True;
end;

In the (Application.ExeName))-part I have put:
ITKnowledgeBase.EXE

But i get undeclared identifier: 'ITKnowledgeBase'
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

by:rfwoolf
ID: 24816256
No no no, where it says 'Application.Exename' don't replace it with your application's exename, leave it exactly as it is
By the way, ExtractFilePath uses SysUtils, just in case you didn't add it to your uses list
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24816263
I forgot something to put in the OnCreate -event:

procedure TForm1.FormCreate(Sender: TObject);
begin
ADOConnection1.Connected:=False;
ADOConnection1.ConnectionString:='provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='+IncludeTrailingPathDelimiter(ExtractFilePath(ITKnowledgeBase.exe))+'test.mdb;';
ADOConnection1.Connected:=True;
end;

Greetings, PK
0
 
LVL 6

Expert Comment

by:FactorB
ID: 24816264
Application.exename is actually a full application path+name, it shouldn't be changed...proper format is:

IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName))

Or translated into ordinary language extract the file path [ExtractFilePath] of my application [Application.ExeName] and use the backslash on the end [IncludeTrailingPathDelimiter].

(Or at least I think that is the meaning :))
0
 
LVL 7

Expert Comment

by:wrwilson
ID: 24816268
Make it a string and remove a parenthesis

IncludeTrailingPathDelimiter("ITKnowledgeBase.exe")+
0
 
LVL 7

Expert Comment

by:wrwilson
ID: 24816286
Never mind my last, not paying attention...
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24816300
It works. One little question, the connectionstring line is a very long line
where can I break the line. Is this possible?
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 24816329
It is not possible because it needs one single string. However, you can always put line breaks in your source code like this:
ADOConnection1.ConnectionString := 'This is an example' +
  ' of multiple lines ' +
 'of text';

..which will add up to 'This is an example of multiple lines of text'
0
 
LVL 6

Accepted Solution

by:
FactorB earned 300 total points
ID: 24816334
procedure TForm1.FormCreate(Sender: TObject);
var
conn_str:string;
begin
conn_str:='provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=';
conn_str:=conn_str+IncludeTrailingPathDelimiter(ExtractFilePath(ITKnowledgeBase.exe));
conn_str:=conn_str+'test.mdb;';
ADOConnection1.Connected:=False;
ADOConnection1.ConnectionString:=conn_str;
ADOConnection1.Connected:=True;
end;
0
 
LVL 6

Expert Comment

by:FactorB
ID: 24816460
I modified your solution and I haven't noticed that I copied ITKnowledgeBase.exe instead of application.exename

Please change

conn_str:=conn_str+IncludeTrailingPathDelimiter(ExtractFilePath(ITKnowledgeBase.exe));

with

conn_str:=conn_str+IncludeTrailingPathDelimiter(ExtractFilePath(Application.Exename));
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

19 Experts available now in Live!

Get 1:1 Help Now