Solved

Change the path to the connection of my database.

Posted on 2009-07-09
17
190 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 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
Technology Partners: 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 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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