Create an Access database at runtime (Views: 33)
Problem/Question/Abstract: How to create an Access database at runtime Answer: Solve 1: Here is an OP function that will do it for you: procedure CreateMSAccessDB(filename: string); var DBEngine, Workspace: Variant; const {Important to use the following constant as is} dbLangGeneral = ''; LANGID = 0x0409; CP = 1252; COUNTRY = '0'; dbVersion30 = 32; begin DBEngine := CreateOleObject('DAO.DBEngine'); {DBEngine := CreateOleObject('DAO.DBEngine.35'); For DAO 3.5} Workspace := DBEngine.Workspaces[0]; try Workspace.CreateDatabase(filename, dbLangGeneral, dbVersion30); except on EOleException do ShowMessage('Database already exists'); end; end; Solve 2: It's very simple to create a empty Access-Database (*.mdb File) using OLE. It's not necessary to have MS-Access installed on your computer. If an exception occures the error message will returned. After creating the DB you can create Tables with simple SQL-Statements. uses comobj, sysutils; function CreateAccessDatabase(FileName: string): string; var cat: OLEVariant; begin result := ''; try cat := CreateOleObject('ADOX.Catalog'); cat.create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + Filename + ';'); cat := NULL; except on e: Exception do result := e.message; end; end; |