PRB: Cannot Connect Data Control to a Password-Protected Access Database (306429)



The information in this article applies to:

  • Microsoft Access 2000
  • Microsoft Access 2002
  • Microsoft Visual Basic Professional Edition for Windows 6.0

This article was previously published under Q306429

SYMPTOMS

If you attempt to use the Data control to connect to a password-protected Access database at design time, you receive the following error message:
Not a valid password
Specifically, this error occurs when you set the RecordSource property of the Data control.

RESOLUTION

To use the Data control with a password-protected Access 2000 or later database, you must set the Recordset property of the data control at run time as follows:
  1. In your project, click References from the Project menu, and then select the Microsoft DAO 3.6 Object Library check box.
  2. In the Form_Load event of your form that contains the Data control, place the following code where "Data1" is the name of your Data control. Make sure to change the path, table name, and password in the following code to reference your database.
    Private Sub Form_Load()
        Dim db As DAO.Database
        Dim ws As DAO.Workspace
        Dim rst As DAO.Recordset
        Set ws = DBEngine.Workspaces(0)
        Set db = ws.OpenDatabase _
            ("C:\Atest.mdb", _
            False, False, "MS Access;PWD=aaa")
        Set rst = db.OpenRecordset("Table1", dbOpenDynaset)
    
        Set Data1.Recordset = rst
    End Sub
    					

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. From the Project menu, click References, and then select the Microsoft DAO 3.6 Object Library check box.
  3. In the Form1 form module, paste the following code:
    Private Sub Form_Load()
    
       Dim DB As Database
       Dim tblDef As TableDef, fld As Field
    
       Set DB = DBEngine.Workspaces(0).CreateDatabase("C:\Atest.mdb", _
           dbLangGeneral, dbEncrypt)
       DB.NewPassword "", "aaa"
       ' Create new TableDef.
       Set tblDef = DB.CreateTableDef("Table1")
       ' Add field to tblDef.
       Set fld = tblDef.CreateField("Field1", dbInteger)
       tblDef.Fields.Append fld
       ' Append TableDef definition to TableDefs collection to save TableDef definition.
       DB.TableDefs.Append tblDef
       DB.Close
       MsgBox "Atest.mdb and Table1 is created."
    
    End Sub 
    					
  4. Press the F5 key to run the project. A password-protected database named Atest.mdb is created.
  5. Start another instance of Visual Basic. Form1 is again created by default.
  6. Add a Data control to Form1.
  7. In the Properties window, set the DatabaseName property of the Data control to C:\Atest.mdb.
  8. If you try to set the RecordSource property, you receive the above-mentioned error message.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

238401 PRB: Error "Unrecognized Database Format" When You Upgrade to Access 2000 or 2002


Modification Type:MinorLast Reviewed:7/16/2004
Keywords:kbprb KB306429