Other Ways of Connecting and Retrieving Results

As mentioned earlier, there are a number of ways of authenticating an ADO connection to Active Directory. The simplest is the way outlined earlier using the Connection::Provider set with the username and password as second and third arguments:

    Set objConn = CreateObject("ADODB.Connection")
    objConn.Provider = "ADSDSOObject"
    objConn.Open "", _
                 "CN=Administrator,CN=Users,dc=mycorp,dc=com", _
                 "mypass"

Because ADO is designed for databases, it is often necessary to specify a number of other requirements when opening a connection. These include a different provider, a different server, or a specific database. All of these items can be set prior to opening the connection. However, none of these make a difference to the AD provider. If you wish to open a connection by setting these values in the ConnectionString property, then do so as shown in the following code:

    Set objConn = CreateObject("ADODB.Connection")
    objConn.Provider = "ADSDSOObject"
    objConn.ConnectionString = _
      "DSN=;UID=CN=Administrator,CN=Users,dc=mycorp,dc=com;PWD=mypass"
    objConn.Open

Semicolons separate the arguments, with the expected DataSourceName (DSN) specified as empty at the start of the string.

One important point: do not authenticate using both methods with the same connection — use one or the other. The following code uses both methods to illustrate what not to do:

 Set objConn = CreateObject("ADODB.Connection") objConn.Provider = "ADSDSOObject" objConn.Open _ "DSN=;UID=CN=Administrator,CN=Users,dc=mycorp,dc=com;PWD=mypass", ...

Get Active Directory, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.