Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q306429

↑ Back to the top


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.

↑ Back to the top


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
    					

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


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.

↑ Back to the top


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

↑ Back to the top


Keywords: KB306429, kbprb

↑ Back to the top

Article Info
Article ID : 306429
Revision : 3
Created on : 7/16/2004
Published on : 7/16/2004
Exists online : False
Views : 294