Microsoft KB Archive/306429

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

Article ID: 306429

Article Last Modified on 7/16/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition
 * Microsoft Access 2002 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



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:  In your project, click References from the Project menu, and then select the Microsoft DAO 3.6 Object Library check box.  In the Form_Load event of your form that contains the Data control, place the following code where &quot;Data1&quot; 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 _ (&quot;C:\Atest.mdb&quot;, _       False, False, &quot;MS Access;PWD=aaa&quot;) Set rst = db.OpenRecordset(&quot;Table1&quot;, dbOpenDynaset)

Set Data1.Recordset = rst End Sub 



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Start a new project in Visual Basic. Form1 is created by default. From the Project menu, click References, and then select the Microsoft DAO 3.6 Object Library check box.  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(&quot;C:\Atest.mdb&quot;, _      dbLangGeneral, dbEncrypt) DB.NewPassword &quot;&quot;, &quot;aaa&quot; ' Create new TableDef. Set tblDef = DB.CreateTableDef(&quot;Table1&quot;) ' Add field to tblDef. Set fld = tblDef.CreateField(&quot;Field1&quot;, dbInteger) tblDef.Fields.Append fld ' Append TableDef definition to TableDefs collection to save TableDef definition. DB.TableDefs.Append tblDef DB.Close MsgBox &quot;Atest.mdb and Table1 is created.&quot;

End Sub </li> Press the F5 key to run the project. A password-protected database named Atest.mdb is created.</li> Start another instance of Visual Basic. Form1 is again created by default.</li> Add a Data control to Form1.</li> In the Properties window, set the DatabaseName property of the Data control to C:\Atest.mdb.</li> If you try to set the RecordSource property, you receive the above-mentioned error message.</li></ol>

<div class="references_section">