Microsoft KB Archive/129880

= PRB: No Error On Invalid SetDefaultWorkspace in VB 4.0 =

Article ID: 129880

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q129880



SYMPTOMS
Executing the SetDefaultWorkspace command with invalid parameters does not generate any error until a Database is opened or a Workspace is created. However, in Visual Basic version 3.0, this error is generated by the SetDefaultWorkspace statement.

The SetDefaultWorkspace statement establishes the user ID and password for protected (security-enabled) Microsoft Jet databases. If you aren't using a protected database, this statement is ignored.



CAUSE
In Visual Basic version 3.0, the SetDefaultWorkspace command immediately attempted to verify the parameters passed to it for the current session. In Visual Basic 4.0, the Username and Password parameters passed to the SetDefaultWorkspace statement set properties on the DBEngine object. These properties are not applied until a Workspace is created or a Database is opened.



RESOLUTION
The SetDefaultWorkspace statement is included in Visual Basic version 4.0 for compatibility with earlier versions. For Visual Basic version 4.0 applications, Microsoft recommends that you use the properties of the Workspace object instead. See the "Example Resolution" section near the end of this article for an example.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
To reproduce the SetDefaultWorkspace error, you must have a password protected Microsoft Access database. Visual Basic version 4.0 does not ship with a secure database. If you have Microsoft Access, you can add security to a database by opening the database and choosing Change Password from the Security menu.

 Start a new project in Visual Basic. Form1 is created by default.  Add the following code to the Form1_Click event procedure: Sub Form1_Load Dim DB As Database SetDefaultWorkspace "Admin", "WrongPassword" ' VB3: Error occurs on SetDefaultWorkspace Set DB = OpenDatabase("MyDB.MDB") ' Insert the name of a secured Microsoft Access database in     ' the line above. End Sub  Start the program by choosing Start from the Run menu or by pressing the F5 key.

In Visual Basic version 3.0, this will generate an immediate error on the SetDefaultWorkspace statement because there is no existing System.MDA.

In Visual Basic version 4.0, the error does not occur until you attempt to open a secure database. Replace the above code with the following.

NOTE: To run this sample you must have the Microsoft DAO 2.5/3.0 Compatibility Library selected in the Tools References dialog. Sub Form1_Load Dim DB As Database SetDefaultWorkspace "Admin", "WrongPassword" Set DB = DBEngine.Workspaces.OpenDatabase("MyDB.MDB") ' Insert the name of a secured Microsoft Access database ' in the line above. ' VB4: Error occurs on OpenDatabase. End Sub

Example Resolution
The implication here is that you may need to move your error trapping code if you are currently trapping for errors on the SetDefaultWorkspace statement.

To update your code to use the correct Microsoft DAO 3.0 syntax, replace the above version 4.0 example with the following: Sub Form1_Click Dim WS As WorkSpace Dim DB As Database DBEngine.DefaultUser = "Admin" DBEngine.DefaultPassword = "WrongPassword" Set WS = DBEngine.CreateWorkSpace("WS") Set DB = WS.Opendatabase("MyDB.MDB") ' Insert the name of a secured Microsoft Access database in the ' line above. End Sub Error trapping may have to be moved in order to accommodate the multiple Workspace capability of Visual Basic version 4.0. Error trapping should be set up to trap on the Opendatabase or CreateWorkspace methods.

Additional query words: 4.00 vb4win vb4all

Keywords: kbprb KB129880

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.