Partager via


ActiveConnection Property

Topic Last Modified: 2006-11-30

Returns the currently active Microsoft® ActiveX® Data Objects (ADO) Connection object.

Applies To

IDataSource Interface

Type Library

Microsoft CDO for Exchange 2000 Library

DLL Implemented In

CDOEX.DLL

Syntax

Property ActiveConnection As ADODB.Connection

HRESULT get_ActiveConnection(_Connection** pVal);
HRESULT put_ActiveConnection(_Connection* Val);

Parameters

  • pVal
    Returned reference to a _Connection Interface.
  • Val
    Sets the value of the ActiveConnection property to the value of the _Connection Interface.

Remarks

This property contains the _Connection interface on an ADO Connection object associated with the currently bound data source. This connection exists only after directly binding the object to a data source specified using a URL (using an OLE DB 2.5 Provider) with calls to the Open Method, the SaveTo Method (DataSource), and the SaveToContainer Method.

An ActiveConnection object reference is not available when the object is bound to another object after using the OpenObject Method or the SaveToObject Method, even if that object has an associated Connection object. Attempting to access the ActiveConnection property in such cases will cause the exception to be raised. For example, if you use IDataSource.OpenObject to bind a Collaboration Data Objects (CDO) object to an ADO Record object that is itself currently bound to an item in the Exchange store or Microsoft Active Directory®, the Record object's Connection object is not available through the IDataSource.ActiveConnection property.

Example

This Microsoft Visual Basic® example uses a file URL with the Microsoft ExchangeOLE DB (ExOLEDB) provider. The ExOLEDB provider also supports The HTTP: URL Scheme.

' Reference to Microsoft CDO for Exchange 2000 Library
' Reference to Microsoft ActiveX Data Objects 2.5 Library

    Dim iFldr As New Folder
    Dim iDsrc As CDO.IDataSource
    Set iDsrc = iFldr

    ' Open the folder. For example:
    ' Url1 = "file://./backofficestorage/example.com/MBX/useralias/"

    Dim Conn As New ADODB.Connection
    Conn.Provider = "ExOLEDB.DataSource"
    Conn.Open Url1

    iDsrc.Open Url1, Conn, adModeReadWrite

    Dim iMsg As New CDO.message
    With iMsg
        .To = "someone@example.com"
        .From = "another@example.com"
        .Subject = "Here is the subject"
        .TextBody = "Here is the text of the message"
    End With

    Set iDsrc = iMsg

    ' Note: it is possible to overwrite an existing item using adCreateOverwrite.
    iDsrc.SaveTo URL1 & "/Drafts/item8.eml", _
                Conn, _
                adModeReadWrite, _
                adCreateOverwrite

    Conn.Close
    Set Conn = Nothing

// You must have the following paths in your INCLUDE path.
// %CommonProgramFiles%\system\ado
// %CommonProgramFiles%\microsoft shared\cdo

#ifndef _CORE_EXAMPLE_HEADERS_INCLUDED
#define _CORE_EXAMPLE_HEADERS_INCLUDED
#import <msado15.dll> no_namespace
#import <cdoex.dll> no_namespace
#include <iostream.h>
#endif

// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
void SaveMessageToDrafts(bstr_t mailboxURL, IMessagePtr pMsg) {

   IDataSourcePtr pDsrc;
   IDataSourcePtr pDsrc2;
   _ConnectionPtr pConn(__uuidof(Connection));
   IFolderPtr     pFolder(__uuidof(Folder));
   FieldsPtr      Flds;
   FieldPtr       Fld;
   variant_t      vDraftsURL;

   // You may need the following for passing optional variants.
   _variant_t varOpt(DISP_E_PARAMNOTFOUND,VT_ERROR);

   pConn->Provider = "ExOLEDB.DataSource";
   try {
      pConn->Open(mailboxURL,bstr_t(),bstr_t(),-1);
   }
   catch(_com_error e) {
      throw e;
   }

   // Bind to the information store root folder (private/messages).
   // For example, URL1 below is something like
   // "file://./backofficestorage/example.com/storename/username/"

   cout << "Opening " << mailboxURL << endl;

   pDsrc = pFolder;

   try {
      pDsrc->Open(
         mailboxURL,
         _variant_t((IDispatch*)pConn, true),
         adModeRead,
         adFailIfNotExists,
         (RecordOpenOptionsEnum)adOpenSource,
         bstr_t(),
         bstr_t()
         );

      Flds = pFolder->Fields;
      Fld = Flds->Item[variant_t("urn:schemas:httpmail:drafts")];
      vDraftsURL = Fld->Value;
   }
   catch(_com_error e) {
      cerr << "Error opening Mailbox folder" << endl;
      throw e;
   }

      // Get ActiveConnection for the bound collection resource
      // (the root of private folders for the user).

   pConn = pDsrc->ActiveConnection;

   // Get IDataSource on the passed message object.
   pDsrc2 = pMsg;

   // Save this message to some folder.
   // Use the current connection to bind the above folder.

   try {
      pDsrc2->SaveToContainer(
            bstr_t(vDraftsURL),

            //Connection from Folder Bind above
            _variant_t((IDispatch*)pConn, true),
            adModeReadWrite,
            adCreateNonCollection,
            (RecordOpenOptionsEnum) NULL,
            bstr_t(),
            bstr_t()
      );
   }
   catch(_com_error e) {
      cerr << "Error saving message to Drafts folder!" << endl;
      throw e;
   }

   // Close the connection.
   pConn->Close();
}

' This example demonstrates sharing a Connection object from a
' bound CDO Folder object and a CDO Message object.
'
' Type information must have been imported into the scripting engine,
' for example:
' <reference object="adodb.record"/>
' <reference object="cdo.message"/>
' elements in a Windows Script Host (.wsf) file.

Function SaveMessageToSubFolder(iMsg, iFldr1, relNewFolderURL)

    Dim iDsrc_Msg
    Dim iDsrc_Fldr1
    Dim iDsrc_Fldr2

    Dim iFldr2
    Set iFldr2 = CreateObject("CDO.Folder")

    ' First create the new sub-folder using the currently bound Folder object's connection:
    Set iDsrc_Fldr1 = iFldr1.DataSource
    Set iDsrc_Fldr2 = iFldr2.DataSource

    On Error Resume Next
    iDsrc_Fldr2.Open iDsrc_Fldr1.SourceURL & "/" & relNewFolderURL, _
                     iDsrc_Fldr1.ActiveConnection, _
                     adModeReadWrite

    If Err.Number <> 0 Then
     ' Folder must not exist...
     Err.Clear
     Err.Number = 0
     iDsrc_Fldr2.SaveTo iDsrc_Fldr1.SourceURL & "/" & relNewFolderURL, _
                        iDsrc_Fldr1.ActiveConnection, _
                        adModeReadWrite

     If Err.Number <> 0 Then
       wscript.echo "Error creating folder: " & Err.Description
       ' Handle Error and Quit
       wscript.quit
     End If
    End If

    On Error Goto 0

    ' Save the message to the folder as say "item8.eml"
    Set iDsrc_Msg = iMsg.DataSource

    ' Note: it is possible to overwrite an existing item using adCreateOverwrite.
    iDsrc_Msg.SaveTo iDsrc_Fldr2.SourceURL & "/item8.eml", _
                iDsrc_Fldr2.ActiveConnection, _
                adModeReadWrite, _
                adCreateOverwrite

    Set SaveMessageToSubFolder = iFldr2
End Function

See Also

Concepts

IDataSource Interface