PRB: No Error On Invalid SetDefaultWorkspace in VB 4.0

ID: Q129880

4.00 WINDOWS kbprg kbprb

The information in this article applies to:

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.

MORE INFORMATION

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.

1. Start a new project in Visual Basic. Form1 is created by default.

2. 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

3. 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 reference words: 4.00 vb4win vb4all KBCategory: kbprg kbprb KBSubcategory: APrgDataAcc

Keywords          : APrgDataAcc 
Version           : 4.00
Platform          : WINDOWS

Last Reviewed: April 17, 1996