Re: Importing ADOX library - How?



Pontus Berg wrote:
I would hope you are right but please tell me
To import (in D7) go to Project|Import type library.

The following works on Access 2000, not tested on any other versions. It came from a very comprehensive example I found on the web, and now can't locate.

a) how to add a field in the database at runtime
Import the .tlb from msadox.dll and put it in your uses clause.

procedure TForm1.Button1Click(Sender: TObject);
var
cat: _Catalog;
col : _Column;
table : _Table;
I,j: Integer;
fName, cStr: olevariant;
begin
{
adGUID is an Access AutoNumber with a Field Size of 'Replication ID'
instead of 'Long Integer'.
Type_ := adGUID;
Specify whether a GUID should be automatically generated for the
column. This property is ignored unless the column type is adGUID.
Properties['Jet OLEDB:Autogenerate'].Value := true;
lTable.Columns.Append "Author", adVarWChar, 50
lTable.Columns("FileName").Properties("Jet OLEDB:Allow Zero Length") = True
lTable.Columns.Append "DateAdded", adDate
lTable.Columns.Append "ShortDesc", adLongVarWChar
lTable.Columns.Append "Display", adSingle
lTable.Columns.Append "Priority", adInteger
lTable.Columns.Append "Feature", adSingle
Type_ := adDouble;
}

cStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DBName + ';Jet OLEDB:Engine Type=5';
cat := CoCatalog.Create;
with cat do
begin
_Set_ActiveConnection(cStr);
end;
table := CoTable.Create;
table := cat.Tables['Table_Name'];
col := CoColumn.Create;
fname := 'My_New_Field';
with col do
begin
ParentCatalog := cat;
i := table.Columns.Count;
Name := fName;
Type_ := adVarWChar;
DefinedSize := 15;
Properties['Jet OLEDB:Allow Zero Length'].Value := true;
Properties['Nullable'].Value := true;
i := table.Columns.Count;
table.Columns.Append(col, Unassigned, Unassigned);
end;
table := CoTable.Create;
table := cat.Tables['Table_Name'];
col := CoColumn.Create;
with col do
begin
ParentCatalog := cat;
i := table.Columns.Count;
Name := fName;
Type_ := adInteger;
Properties['Jet OLEDB:Allow Zero Length'].Value := true;
Properties['Default'].Value := 0;
i := table.Columns.Count;
table.Columns.Append(col, Unassigned, Unassigned);
end;
end;

b) how to compact the database
Import the tlb from msjro.dll and put it in your uses clause(JRO_TLB).

procedure TfrmLog.mnuCompactDBClick(Sender: TObject);
var
wholePath: string;
JE: TJetEngine;
const
SProvider1 = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=';
begin
wholePath := WritePath + FOLDER_NAME + DBName; //fully qualified path
if FileExists(WritePath + FOLDER_NAME + 'compacted.mdb') then
DeleteFile(WritePath + FOLDER_NAME + 'compacted.mdb');
if FileExists(wholePath) then
begin
JE:= TJetEngine.Create(Application);
dbDAO.close; /// close the database
try
try
JE.CompactDatabase(SProvider1 + wholePath, SProvider1 + WritePath + FOLDER_NAME + 'compacted.mdb');
except
on E:Exception do
ShowMessage(E.Message);
end;
finally
JE.FreeOnRelease;
if FileExists(WritePath + FOLDER_NAME + 'compacted.mdb') then
begin
DeleteFile(wholepath);
RenameFile(WritePath + FOLDER_NAME + 'compacted.mdb', wholepath);
end;
if FileExists(wholePath) then
begin
//open the new db
ShowMessage('Compact/Repair succeded.');
end;
end;
end;
end;

HTH
Mike
.


Quantcast