Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Change the path to the connection of my database.

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
SOLUTION
Avatar of Randy Wilson
Randy Wilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Kiers

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"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';
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;
Avatar of FactorB
FactorB

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.
Ups, again something similar was posted during the time I was typing, I should take touch typing classes :)
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'
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
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
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 :))
Make it a string and remove a parenthesis

IncludeTrailingPathDelimiter("ITKnowledgeBase.exe")+
Never mind my last, not paying attention...
It works. One little question, the connectionstring line is a very long line
where can I break the line. Is this possible?
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'
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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));