试试:D5
先导入ADO的JRO_TLB(Project->Import Type Libary->
Microsoft Jet and replication Object 2.5 Libaray),
然后:
unit ADO_Access;
interface
uses
SysUtils, Forms, FileCtrl, Windows, Classes,
ComObj, JRO_TLB;
function CreateMDB(const MDBName: string; const PWD: string = '';
const MDBType: string = '97'): Boolean;
var
ADOVar: OleVariant;
sCreateString: string;
begin
Result := False;
//Check file and path exist
if FileExists(MDBName) then
begin
raise Exception.Create(MDBName + '已经存在!');
Exit;
end;
if not DirectoryExists(ExtractFilePath(MDBName)) then
begin
raise Exception.Create('文件路径:' + #13 + ExtractFilePath(MDBName) + #13
+ '不存在!');
Exit;
end;
//Set create string
sCreateString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=';
sCreateString := sCreateString + MDBName + ';';
if MDBType = '97' then
sCreateString := sCreateString + 'Jet OLEDB:Engine Type=4'
else if MDBType = '2000' then
sCreateString := sCreateString + 'Jet OLEDB:Engine Type=5'
else begin
raise Exception.Create('本函数不支持建立其他格式的MS Access数据库!');
Exit;
end;
if PWD <> '' then
sCreateString := sCreateString + ';Jet OLEDB
atabase Password=' + PWD;
//Create ADOX object
try
ADOVar := CreateOleObject('ADOX.Catalog');
ADOVar.Create(sCreateString);
Result := True;
except
raise Exception.Create('无法新建 MS Access ' + MDBType + ' 数据库:' + #13
+ MDBName);
end;
end;