Article ID: 149594
Article Last Modified on 11/21/2006
COleVariant varOldName;
COleVariant varNewName( _T("NewUser"), VT_BSTRT );
// Code to assign pUser to a valid value omitted
DAOUser *pUser = NULL;
// These method declarations were taken from Dbdaoint.h
// STDMETHOD(get_Name) (THIS_ BSTR FAR* pbstr) PURE;
// STDMETHOD(put_Name) (THIS_ BSTR bstr) PURE;
DAO_CHECK( pUser->get_Name ( &V_BSTR(&varOldName) ) );
DAO_CHECK( pUser->put_Name ( V_BSTR (&varNewName) ) );
NOTE: VT_BSTRT is required in the constructor of COleVariant as
described in the following article in the Microsoft Knowledge Base:
140599 PRB: MFC DAO Functions Accepting COleVariant Strings May Fail
COleVariant varName( _T( "MyName" ), VT_BSTRT ); CString str = V_BSTRT( &varName );The V_BSTRT macro, along with other tricks to crack open other types stored within COleVariant, is demonstrated in the DAOView sample (DAOView is included with Visual C++ 4.x). Specifically, this translation is performed in the CCrack::strVARIANT method. This method, where possible, translates the value of a COleVariant into an instance of CString.
// The following code presents several functions you can use to manage
// MFC DAO classes in a way that allows you to call the DAO interfaces.
// It then shows how to use those functions within InitInstance().
// Set the system database that the DAO database engine will use:
void AfxDaoSetSystemDB(CString & strSystemMDB)
{
COleVariant varSystemDB(strSystemMDB,VT_BSTRT );
// Initialize DAO for MFC:
AfxDaoInit();
DAODBEngine* pDBEngine = AfxDaoGetEngine();
ASSERT(pDBEngine != NULL);
// Call put_SystemDB method to set the system database for DAO engine:
DAO_CHECK( pDBEngine->put_SystemDB( varSystemDB.bstrVal ) );
}
void AfxDaoSetDefaultUser(CString & strUserName, CString & strPassword)
{
COleVariant varUserName( strUserName, VT_BSTRT );
COleVariant varPassword( strPassword, VT_BSTRT );
DAODBEngine* pDBEngine = AfxDaoGetEngine();
ASSERT(pDBEngine != NULL);
// Set default user:
DAO_CHECK( pDBEngine->put_DefaultUser(varUserName.bstrVal));
// Set default password:
DAO_CHECK( pDBEngine->put_DefaultPassword(varPassword.bstrVal));
}
void AfxDaoChangePassword(CString & strUserName, CString &
strOldPassword, CString & strNewPassword)
{
// Create(Open) Workspace:
CDaoWorkspace wsp;
CString strWspName = _T("Temp Workspace");
wsp.Create( strWspName, strUserName, strOldPassword );
wsp.Append();
// Determine how many objects there are in the Users collection:
short nUserCount;
short i;
DAOUser *pUser = NULL;
DAOUsers *pUsers = NULL;
// Side-effect is implicit OLE AddRef() on DAOUser object:
DAO_CHECK( wsp.m_pDAOWorkspace->get_Users( &pUsers ) );
// Side-effect is implicit OLE AddRef() on DAOUsers object:
DAO_CHECK( pUsers->get_Count( &nUserCount ) );
TRACE( "# of users in collection = %d\n", nUserCount );
// Traverse through the list of users and change password for userid
// used to create/open the workspace:
for( i = 0; i < nUserCount; i++ )
{
COleVariant varIndex( i, VT_I2 );
COleVariant varName;
// Retrieve information for user i
DAO_CHECK( pUsers->get_Item( varIndex, &pUser ) );
// Retrieve name for user i
DAO_CHECK( pUser->get_Name( &V_BSTR(&varName)) );
TRACE( "User # %d Name = %s\n", i, V_BSTRT(&varName) );
CString strTemp = V_BSTRT(&varName);
// If there is a match, change the password:
if( strTemp == strUserName )
{
COleVariant varOldPwd( strOldPassword, VT_BSTRT );
COleVariant varNewPwd( strNewPassword, VT_BSTRT );
DAO_CHECK( pUser->NewPassword( V_BSTR( &varOldPwd ),
V_BSTR( &varNewPwd ) ) );
TRACE( "\t Password is changed\n" );
}
}
// Clean up: decrement the usage count on the OLE objects:
pUser->Release();
pUsers->Release();
wsp.Close();
}
BOOL CMyApp::InitInstance()
{
// Specify path to the Microsoft Access system database:
CString strSystemDB =
_T("c:\\Program Files\\MSOffice\\access\\System.mdw");
// Set system database before MFC initilizes DAO:
AfxDaoSetSystemDB(strSystemDB);
// User name and password manually added by using Microsoft Access:
CString strUserName = _T("NewUser");
CString strOldPassword = _T("Password");
CString strNewPassword = _T("NewPassword");
// Set default user so that MFC will be able to log in by default
// using the user name and password from the system database:
AfxDaoSetDefaultUser(strUserName,strOldPassword);
// Change the password. You should be able to call this function from
// anywhere in your MFC application:
ChangePassword(strUserName,strOldPassword,strNewPassword);
// Remainder of your InitInstance code:
...
}
146876 PRB: DAO Error 3028 When Creating a DAO Workspace
140599 PRB: MFC DAO Functions Accepting COleVariant Strings May Fail
143408 BUG: Assertion Failure After Using CDaoWorkspace::Create()
Keywords: kbcode kbdatabase kbhowto kbprogramming KB149594