0

Cannot open the MS Office Access database engine workgroup information file - When I have code as posted.

What I am trying to do in my code is to create MS Access 2007 file and then set the user name and password to it from my program. What am I doing wrong here?

Error occurs here: objOleDbConnection.Open();

EDIT: I have made some changes, seems like it opens a connection but the command is incorrect.

Now problem is here:

        objOleDbCommand.CommandText = 
            "ALTER USER " + storedAuth.UserName + 
            " PASSWORD [" + storedAuth.Password + "] []";

The entire code:

    // Creating an object allowing me connecting to the database.
    OleDbConnection objOleDbConnection = new OleDbConnection();
    objOleDbConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + sfdNewFile.FileName + ";Persist Security Info=False";
    // Creating command object.
    OleDbCommand objOleDbCommand = new OleDbCommand();
    objOleDbCommand.Connection = objOleDbConnection;

    try
    {
        objOleDbConnection.Open();
        objOleDbCommand.CommandText = "ALTER USER " + 
                    storedAuth.UserName + " PASSWORD [" + 
                    storedAuth.Password + "] []";
        objOleDbCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        // Displaying any errors that 
        // might have occured.
        MessageBox.Show("Error: " + ex.Message);
    }
    finally
    {
        objOleDbConnection.Close();
    }
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • 1
    Is this an MDB or ACCDB file? You mentioned this is an Access 2007 file, but I want to be absolutely clear since [ACCDB files don't have user-level security](http://office.microsoft.com/en-us/access-help/what-happened-to-user-level-security-HA010234561.aspx). – Cheran Shunmugavel Nov 25 '11 at 07:42
  • @Cheran Shunmugavel: Does MDB have it? I do use ACCDB. – HelpNeeder Nov 25 '11 at 08:04
  • 1
    yes. Make sure you're not confusing user-level security (which is only available with an MDB) with a simple database password (which is available with either MDB or ACCDB). If you just want to password protect the database, you would use the [`ALTER DATABASE PASSWORD`](http://msdn.microsoft.com/en-us/library/bb177884%28v=office.12%29.aspx) command instead. – Cheran Shunmugavel Nov 25 '11 at 08:08
  • @Cheran Shunmugavel: I do want to be able to have username AND password. Could you please point some articles I could refer to? I was googling but found nothing that would tell me how to do this. – HelpNeeder Nov 25 '11 at 08:18
  • 1
    [About user-level security](http://office.microsoft.com/en-us/access-help/about-user-level-security-mdb-HP003070410.aspx?CTT=1) – Cheran Shunmugavel Nov 25 '11 at 08:23

3 Answers3

0

Well, the error you are getting suggests someone else is keeping the file open, which prevents the password change...

zmbq
  • 38,013
  • 14
  • 101
  • 171
0

To change an Access DB password, you must it open in exclusive mode. Try adding this to your connection string ;Exclusive=1.

createMSFile.Create("Provider=Microsoft.ACE.OLEDB.12.0;Exclusive=1;Data Source=" +
        sfdNewFile.FileName);
Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104
  • If I add this i get an error: Could not find installable ISAM. – HelpNeeder Nov 25 '11 at 06:16
  • PS the line you have there is line that creates a file, which is fine. I have a problem with other line of code. Look at my OP. I have made a mistake earlier posting the line you are talking about, but I have corrected it. – HelpNeeder Nov 25 '11 at 06:20
  • okk ....then remove the Exclusive from above line and insert `OleDbConnection objOleDbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Exclusive=1" + "Data Source=" + sfdNewFile.FileName);` ... and see if it works – Pankaj Upadhyay Nov 25 '11 at 06:22
  • Yes, I have figured that out and I did use exclusive part where it should be. That's where I do have a problem. – HelpNeeder Nov 25 '11 at 06:30
-1

HelpNeeder, I think the problems you are experiencing should first be solved in your other question: Error tells me I haven't close the connection, but haven't I?

Thanks!

Community
  • 1
  • 1
Pavel Donchev
  • 1,809
  • 1
  • 17
  • 29