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