Solved

Change the path to the connection of my database.

Posted on 2009-07-09
17
189 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: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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

774 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