Example for Real Error Messages and Error Trapping for ADOX type lib
For anyone who has been banging their head against a desk looking for an
answer to using ADOX in CBuilder, here are some solid examples. This
example code demonstrates:
1) Creating a New Access MDB.
2) Creating new tables and columns.
3) Working with Jet Specific Table Properties
4) Creating Linked Tables in Access thru ADO
Note:
In order to use this code in cb you will have to import the ADOX type lib
To do so goto Project->Import Type Library->and choose ADO Ext. 2.5 or 2.6
for DDL and Security.
Once you have done this you now have a useable type library for ADOX
available in CB.
Now here's the code. I hope this helps. :^)
bool __fastcall TForm1::CreateMDB(AnsiString FileName)
{
TCOM_Catalog XCatalog;
WideString ConnectString;
try
{
XCatalog = CoCatalog::Create();
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
WideString(FileName);
XCatalog->Create((BSTR)ConnectString);
if (FileExists(FileName))
{
return true;
}
else
{
return false;
}
}
catch(Exception &error)
{
ShowMessage(error.Message.c_str());
return false;
}
//-------------------------------------------------------------------------
-
-
bool __fastcall TForm1::AppendTable(AnsiString FileName)
{
TCOM_Catalog XCatalog;
WideString ConnectString;
WideString TableName;
TCOM_Table XTable;
try
{
XCatalog = CoCatalog::Create();
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
WideString(FileName);
XCatalog->set_ActiveConnection((BSTR)ConnectString);
TableName = "TestTable";
XTable = CoTable::Create();
XTable->set_Name((BSTR)TableName);
XTable->Columns->Append("Col1", adInteger, 0);
XTable->Columns->Append("Col2", adBoolean, 0);
XCatalog->Tables->Append((IUnknown*)XTable);
return true;
}
catch(Exception &error)
{
ShowMessage(error.Message.c_str());
return false;
}
//--------------------------------------------------------------------------
-
bool __fastcall TForm1::AppendLinkedTable(AnsiString
SourceFileName,AnsiString SourceTableName,AnsiString
DestinationFileName,AnsiString DestinationTableName)
{
TCOM_Catalog XCatalog;
WideString ConnectString;
TCOM_Table XTable;
try
{
XCatalog = CoCatalog::Create();
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
WideString(DestinationFileName);
XCatalog->set_ActiveConnection((BSTR)ConnectString);
XTable = CoTable::Create();
XTable->set_ParentCatalog(XCatalog);
XTable->set_Name((BSTR)WideString(DestinationTableName));
XTable->Properties->get_Item((BSTR)WideString("Jet OLEDB:Link
Datasource"))->set_Value((BSTR)WideString(SourceFileName));
XTable->Properties->get_Item((BSTR)WideString("Jet OLEDB:Link
Provider String"))->set_Value((BSTR)WideString("MS Access"));
XTable->Properties->get_Item((BSTR)WideString("Jet OLEDB:Remote
Table Name"))->set_Value((BSTR)WideString(SourceTableName));
XTable->Properties->get_Item((BSTR)WideString("Jet OLEDB:Create
Link"))->set_Value(Variant(true));
XCatalog->Tables->Append((IUnknown*)XTable);
return true;
}
catch(Exception &error)
{
ShowMessage(error.Message.c_str());
return false;
}
//--------------------------------------------------------------------------
-
Bill