Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change the path to the connection of my database.

Posted on 2009-07-09
17
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
17 Comments
 
LVL 7

Assisted Solution

by:Randy Wilson
Randy Wilson earned 400 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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:Randy Wilson
ID: 24816268
Make it a string and remove a parenthesis

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

Expert Comment

by:Randy Wilson
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 1200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

721 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