Vrtnar
asked on
Open database at runtime
My database is in my app.exe folder (and I want my app. to allways look for it there and display error otherwise).
Also I need to tell my app that the table to open is in that database.How?
What is the code supposed to look like?
Also I need to tell my app that the table to open is in that database.How?
What is the code supposed to look like?
ASKER
I am using absolute database (*.abs)
well.. assuming you are using a TDataset descendent such as TTable then you would do
table1.databasename := ExtractFilePath(Paramstr(0 )); // same dir as .exe
if you want to do a search in the current dir and find the files yourself, you can do this :-
procedure TForm1.Button1Click(Sender : TObject);
var
searchrec: tsearchrec;
databasefiles: tstringlist;
searchresult: integer;
begin
databasefiles := TStringList.Create;
try
searchresult := findfirst(extractfilepath( paramstr(0 )) + '*.abs', faAnyFile, SearchRec);
while searchresult = 0 do
begin
if pos('.', SearchRec.Name) <> 1 then
databasefiles.Add(SearchRe c.Name);
searchresult := FindNext(SearchRec);
end;
memo1.lines.assign(databas efiles); // show the list of databse files in a memo
finally
databasefiles.Clear;
databasefiles.Free;
end;
end;
table1.databasename := ExtractFilePath(Paramstr(0
if you want to do a search in the current dir and find the files yourself, you can do this :-
procedure TForm1.Button1Click(Sender
var
searchrec: tsearchrec;
databasefiles: tstringlist;
searchresult: integer;
begin
databasefiles := TStringList.Create;
try
searchresult := findfirst(extractfilepath(
while searchresult = 0 do
begin
if pos('.', SearchRec.Name) <> 1 then
databasefiles.Add(SearchRe
searchresult := FindNext(SearchRec);
end;
memo1.lines.assign(databas
finally
databasefiles.Clear;
databasefiles.Free;
end;
end;
if you have 1 database file (file.abs) you can do a simple check like
if FileExists(ExtractFilePath (Paramstr( 0)) + 'file.abs') then
showmessage('file is here, i can continue')
else
raise exception.create('file.abs does not exist');
if FileExists(ExtractFilePath
showmessage('file is here, i can continue')
else
raise exception.create('file.abs
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
procedure TForm1.FormCreate(Sender: TObject);
var
DataBaseName,DatabaseFileN
begin
Path := ExtractFilePath(Applicatio
DatabaseName:= ExtractFilePath(ParamStr(0
should go something like that...?
ASKER
ExcludeTrailingbackslash is specific to a platform ?!
Nope, included in sysutils and my personal coding preference;
szDbFile:=ExcludeTrailingb ackslash(E xtractFile Path(Appli cation.Exe Name))+'\' +DbFile;
(notice i include the '\' between the path and file name). You can change it to the following if it suits you:
szDbFile:=ExtractFilePath( Applicatio n.ExeName) +DbFile;
---
Russell
szDbFile:=ExcludeTrailingb
(notice i include the '\' between the path and file name). You can change it to the following if it suits you:
szDbFile:=ExtractFilePath(
---
Russell
ASKER
well libby,i did your suggestion and it worked.what i do not understand is the
szDbFile thing.
szDbFile thing.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The szDbFile "thing" is the Application's path PLUS the database file name which are used to build an absolute file name. The only change required in the example I gave was the 2 constansts: database name and table name.
Russell
ASKER
Loki :
procedure TForm1.FormCreate(Sender: TObject);
var
DataBaseName, DatabaseFileName: string;
begin
DatabaseName := 'Mydatabase';
DatabaseFileName:= ExtractFilePath(ParamStr(0 )) + Mydatabase.abs';
if not FileExists() then Raise Exception.Create('File does not exist' + #13#10 + DatabaseFileName);
end else
ABSDatabase1.DatabaseName: = DatabaseName; ABSDatabase1.DatabaseFileN ame:=Datab aseFileNam e;
ABSDatabase1.Open;
ABSTable1.DatabaseName:= DatabaseName;
ABSTable1.TableName:= 'Mytable';
ABSTable1.Open;
end;
I get stopped here : if not FileExists() then
[Error] Unit1.pas(234): Not enough actual parameters
procedure TForm1.FormCreate(Sender: TObject);
var
DataBaseName, DatabaseFileName: string;
begin
DatabaseName := 'Mydatabase';
DatabaseFileName:= ExtractFilePath(ParamStr(0
if not FileExists() then Raise Exception.Create('File does not exist' + #13#10 + DatabaseFileName);
end else
ABSDatabase1.DatabaseName:
ABSDatabase1.Open;
ABSTable1.DatabaseName:= DatabaseName;
ABSTable1.TableName:= 'Mytable';
ABSTable1.Open;
end;
I get stopped here : if not FileExists() then
[Error] Unit1.pas(234): Not enough actual parameters
if not FileExists(DatabaseFileNam
end else
What kind of database are you using (pdox, access, dbase, etc)?
Russell