XL: OpenDatabase Method Prompts for User Input

ID: Q141826

The information in this article applies to:

SYMPTOMS

In Microsoft Excel, when you run a macro that uses the OpenDatabase method to open a database, you may be prompted for information even though the Source argument includes a complete connection string to the data source you are using.

CAUSE

If either the Exclusive (called to Options in version 97) and Read-only arguments are omitted, and the Source argument is included, you will be prompted for any information needed to connect to a data source. The following example illustrates this behavior.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

Before you run the macro, you must create a reference to the DAO Object Library. To do this, activate a module sheet, click References on the Tools menu and select Microsoft DAO 3.5 Object Library (3.0 Object Library in version 7.0).

Sub OpenDB()
    Dim db As Database

    ' Open a connection to the SQL Server data source called
MySQLServer
    Set db = OpenDatabase("", , , "ODBC;DSN=MySQLServer;" _
             & "UID=user1;PWD=pass;DATABASE=Master")
End Sub

In this example, all necessary options to make a connection to the data source called MySQLServer have been specified. However, because the Exclusive (or Options) and Read-only options are missing, you will be prompted for the connection information anyway.

RESOLUTION

When you use the Source argument of the OpenDatabase method, specify a value of True or False for both the Exclusive (Options) and Read-only arguments.

The example above would then read as:

Sub OpenDB()
    Dim db As Database

    ' Open a connection to the SQL Server data source called MySQLServer
    Set db = OpenDatabase("",False ,False , "ODBC;DSN=MySQLServer;" _
             & "UID=user1;PWD=pass;DATABASE=Master")
End Sub

STATUS

This behavior is by design.

MORE INFORMATION

Microsoft Excel Help for the OpenDatabase method states the following for the Source argument:

   You must supply the exclusive and read-only arguments to supply a
   source string.

REFERENCES

For more information about the OpenDatabase method, click Index tab in Help, type the following text

   OpenDatabase method

and double-click the selected text to go to the "Retrieving external data" topic.

Additional query words: 7.00 8.00 97

Keywords          : kbprg kbdta kbdtacode KbVBA xlwin 
Version           : WINDOWS:7.0,97
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999