SoFunction
Updated on 2025-04-09

Get the names of all tables in ACCESS2000 database


void OpenSchemaX(TCHAR *TableName)
{
HRESULT hr = S_OK;

::CoInitialize(NULL); //Initialize Com

IADORecordBinding *picRs = NULL;

_RecordsetPtr pRstSchema("");
_ConnectionPtr pConnection("" );

pConnection->ConnectionString = TableName;
pConnection->Provider = ".4.0";

try
{
pConnection->Open(pConnection->ConnectionString, "", "", adModeUnknown);
pRstSchema->QueryInterface(
__uuidof(IADORecordBinding), (LPVOID*)&picRs);

pRstSchema = pConnection->OpenSchema(adSchemaTables);//Test processing of enumeration table

while(!(pRstSchema->EndOfFile))
{
CString strTableType;

_bstr_t table_name = pRstSchema->Fields->
GetItem("TABLE_NAME")->Value;//Get the name of the table

_bstr_t table_type = pRstSchema->Fields->
GetItem("TABLE_TYPE")->Value;//Get the table type

("%s",(LPCSTR) table_type);

if(!lstrcmp(strTableType,_T("TABLE")))
{
m_strList.AddString((LPCSTR) table_name);//Add the name of the table
}

pRstSchema->MoveNext();
}
// Clean up objects before exit.

pRstSchema->Close();
pConnection->Close();
}

catch (_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Connection.
PrintProviderError(pConnection);
PrintComError(e);
}
CoUninitialize();
}

void PrintProviderError(_ConnectionPtr pConnection)
{
ErrorPtr pErr = NULL;

if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0;i < nCount;i++)
{
pErr = pConnection->Errors->GetItem(i);
CString strError;
("Error number: %x\t%s", pErr->Number, pErr->Description);
AfxMessageBox(strError);
}
}
}

void PrintComError(_com_error &e)
{
_bstr_t bstrSource(());
_bstr_t bstrDescription(());

// Print COM errors.
CString strError;
("Error number: Description = %s\tCode meaning = %s",(LPCSTR) bstrDescription, ());
AfxMessageBox(strError);
}

Calling method:

CString strFileName;
TCHAR FileName[MAX_PATH];
TCHAR bigBuff[2048] = _T(""); // maximum common dialog buffer size
TCHAR szFilter[] = _T("Text Files (*.mdb)|*.mdb|All Files (*.*)|*.*
");
CFileDialog dlg(TRUE, NULL, NULL,
OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, szFilter);

// Modify OPENFILENAME members directly to point to bigBuff
dlg.m_ofn.lpstrFile = bigBuff;
dlg.m_ofn.nMaxFile = sizeof(bigBuff);

if(IDOK == () )
{
strFileName = ();
lstrcpy(FileName,strFileName);
OpenSchemaX(FileName);
} (Source: Fengshan Network Academy)