Search code examples
databasedelphims-accessexistsadox

How to determine IN APP if a .mdb(access database) created from a delphi app already exist or not?


I am writing a delphi project where I am having to create an access database. I am using ADOX for the purpose.

Let me admit, I am a novice when it comes to db handling, and I followed the tutorial on net and successfully created by CreateDatabase Procedure. What I want to do is ensure that a database with the given name doesn't exist and ONLY then create the db.

Procedure CreateDatabase(DB_Name:String);
var
path:String;
dataSource : String;
DB : String;
Begin
  Path := WindowsDirectory;{Its another function}
  delete(Path, 2, length(path));
  DB := Path+':\'+DB_Name+'.mdb';
  DataSource :=
    'Provider=Microsoft.Jet.OLEDB.4.0' +
    ';Data Source=' + DB +
    ';Jet OLEDB:Engine Type=4';
    Form1.ADOXCatalog1.Create1(DataSource);
End;

I've seen this sort of questions on mySql dbs, but none on access dbs nor have I been able to find any solution to the problem.

Pseudo-Code form of what I want :

if (DatabaseExists(DB_Name)) then
   do_something
else 
   create_db

Some help on this issue will be helpful. Thank You :)

Regards Priyabrata Chakraverti


Solution

  • Checking to see if the database exists is simply a matter of checking to see if the database file exists. As Marcus Adams suggested in the comment above, the Delphi function FileExists should do the trick.