Click to See Complete Forum and Search --> : HOW TO: Create a Jet DB With Password Protection


d_wzrdv_z
May 26th, 1999, 11:56 PM
Hi All, I need to programmatically create an Access database with a password, and haven't had much luck.

First I create a workspace :


CDaoWorkspace m_pWorkSpace = new CDaoWorkspace;
CDaoDatabase m_pdbLocal = new CDaoDatabase(m_pWorkSpace);

try
{
m_pWorkSpace->Open("PayMWS");
}
catch (CDaoException * e)
{
//
// Can't open ws, so create it
DAOFail(e, false);;

//
// Cut the password down to what it actually is, e.g. unappend the ;pwd=

CString strPWD = m_strPwd.Right(m_strPwd.GetLength () - (m_strPwd.Find ('=') + 1));

//
// Because we need to specify a password, there needs to be a workspace object

m_pWorkSpace->SetDefaultPassword((LPCTSTR)strPWD);

try
{
m_pWorkSpace->Create(_T("PayMWS"), _T("Admin"), _T(""));
}
catch (CDaoException * e)
{
return DAOFail(e);
}

try
{
m_pWorkSpace->Append();
}
catch (CDaoException * e)
{
return DAOFail(e);
}
}


// ... this works; I have an open ws. next I create a database like this.


try
{
m_pdbLocal->Open("PayMaster", /* will append .mdb */
TRUE, /* Share Exclusive */
FALSE, /* Read Only */
(LPCTSTR)m_strPwd /* Connect string */ );
}
catch (CDaoException* e)
{

//
// Don't call DAOFail() = obviously there is no database
e->Delete();


//
// Create database (nested try)
try
{
m_pdbLocal->Create("PayMaster", dbLangGeneral, dbEncrypt|dbVersion30);
}
catch (CDaoException* e)
{
//
// EXIT APP - (cannot open|create)
return DAOFail(e);
}

fInternOpen = true;
}





It works, but the workspace isn't saved to the engine's collection so I have to create it new every time (I don't have to create the CDaoDatabase each time though that gets saved) and I can easily go into access and open the db without specifying the password.

Can anyone help?

Thank you

d_wzrdv_z
May 27th, 1999, 10:43 PM
Still stuck....tried the CDaoWorkspace static set password member to no avail...

Frederic J.F. Estrampes
June 3rd, 1999, 06:59 PM
May be this is what you are looking for:


void SetDaoPassword( LPCTSTR pDB, LPCTSTR pszOldPassword, LPCTSTR pszNewPassword )
{
CDaoDatabase db;
CString strConnect( _T( ";pwd=" ) );

// the database must be opened as exclusive
// to set a password
db.Open( pDB, TRUE, FALSE,
strConnect + pszOldPassword );

COleVariant NewPassword( pszNewPassword, VT_BSTRT ),
OldPassword( pszOldPassword, VT_BSTRT );

DAO_CHECK( db.m_pDAODatabase->NewPassword( V_BSTR( &OldPassword ),
V_BSTR( &NewPassword ) ) );

db.Close();
}