CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Location
    Melbourne, VIC
    Posts
    72

    HOW TO: Create a Jet DB With Password Protection

    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


  2. #2
    Join Date
    Apr 1999
    Location
    Melbourne, VIC
    Posts
    72

    Re: HOW TO: Create a Jet DB With Password Protection

    Still stuck....tried the CDaoWorkspace static set password member to no avail...


  3. #3
    Join Date
    Mar 1998
    Posts
    3

    Re: HOW TO: Create a Jet DB With Password Protection

    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();
    }





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured