Connecting to the Employee Database

The Employee application displays a dialog box that prompts for the location of Employee.mdb. The application uses this information to connect to the database and open a table-type Recordset object on the Employees table. The application is positioned on the first record in the table. Note that this application is designed to work only with Employee.mdb; specifying another database causes an error (unless that database happens to have a table named Employees). The following code contains the ConnectToDatabase function:

BOOL CDAOEMPDoc::ConnectToDatabase()
{
	CFileDialog		cOpenFile(	TRUE, 
								_T("MDB"), 
								_T("employee.mdb"), 
								OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
								(_T("Access Files (*.mdb) | *.mdb ||")));



	//Get the location of database (assume it's the Employee example).
	cOpenFile.DoModal();

	//Open the database and the recordset.
	try
		{
		//NOTE: Using the default collection rather than Workspaces.Items.
		m_cEmpDatabase = m_cDBEngine.OpenDatabase(cOpenFile.m_ofn.lpstrFile);
		m_cEmpRecordSet = m_cEmpDatabase.OpenRecordset(_T("Employees"));
		m_cEmpRecordSet.MoveFirst();
		}

	catch (CdbException dbExcept)
		{
		CdbLastOLEError exError;
		TCHAR szBuf[256];

		//If error is "No Current Record," assume it's an empty table.
		if(dbExcept.m_hr == E_DAO_NoCurrentRecord)
			{
			m_bEmptyTable = TRUE;
			}
		else
			{
			wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbExcept.m_hr),
				 (LPCTSTR) exError.GetDescription());
			AfxMessageBox(szBuf);
			return (FALSE);
			}
		}


	return TRUE;
}

The C++ calls to the OpenDatabase and OpenRecordset methods are similar to equivalent methods called in Visual Basic. The dbDAO variables behave like objects with associated properties and methods. Collections are also supported; for example, the call to the OpenDatabase method could be rewritten using the Workspaces collection as follows:

m_cEmpDatabase = m_cDBEngine.Workspaces[0L].OpenDatabase(cOpenFile.m_ofn.lpstrFile);

Note that the index is cast as LONG; otherwise, the compiler interprets the “0” as NULL.

See Also For more information about dbDAO properties, methods, and collections, see “dbDAO Syntax” later in this chapter.

To handle exceptions raised by errors, the dbDAO code is nested in a try-catch block. For example, if the Employees table is not found in the database, the call to the OpenRecordset method throws an exception. Execution immediately switches to the catch block, which in this case displays a dialog box containing information about the error. The DBERR macro is used to convert the error from an HRESULT to a Visual Basic error number. For more information about errors, see “Handling dbDAO Errors” later in this chapter.

Strings in the ConnectToDatabase function are wrapped in the _T macro. Using this macro allows the code to compile for both ANSI and Unicode. The dbDAO class run-time libraries are available in both ANSI and Unicode versions. For more information, see “ANSI and Unicode Strings” later in this chapter.