An activity diagram for a method that sets (initializes) the ADO connection for a data services component would look as follows:
Since this is a fairly straightforward example, we will walk through the code associated with this activity diagram :
Private Sub SetADOConnection(v_strDatabasePath As String)
Dim objADOConnection As Object
Dim ADOConnection As ADODB.Connection
' Set the error handler On Error GoTo SetADOConnectionError
' Initialize the ADO Connection object
Set objADOConnection = CreateObject("ADODB.Connection")
' For disconnected recordsets, set ADO cursor location to client With objADOConnection .CursorLocation = adUseClient
' Set the ADO connection string
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist " & "Security Info=False;Data Source=" & v strDatabasePath
' Open the connection
.Open End With
'Build the error string SetADOConnectionError:
Dim lngErrorCounter As Long Dim strErrors As String strErrors = Err.Number & ": " & Err.Description
If objADOConnection.Errors.Count > 0 Then
For lngErrorCounter = 0 To objADOConnection.Errors.Count - 1 strErrors = strErrors & _
objADOConnection.Errors(lngErrorCounter).Number & ": " & objADOConnection.Errors(lngErrorCounter).Description & vbCrLf Next lngErrorCounter
' Rasie the error
Err.Raise 2000 + vbObjectError, "SetADOConnection " & Err.Source, _ strErrors
Even for a simple module such as this, it is much easier to understand the flow of the code and what is going on in the code by looking at the activity diagram. This diagram can lead to discussions on the design. To begin with, why do we need a special method to set the ADO connection? From our discussion on the last chapter on ADO connection objects we know that these objects are pooled. We said that we wanted to use them and then throw them back in the pool. This means that we will be opening our connection, using it closing, and then opening it again, etc. Thus, every time we need to connect we will need to open a Connection object. Because of connection object pooling, we are not actually opening a connection but taking an existing connection out of the pool.
We can also see there are a few properties that will need to be set on the connection object. One of these is the cursor location. The cursor location can be on the server or on the client. A detailed discussion of these is beyond the level of this book, but they both are used in different situations for different types of data access. The activity diagram makes it clear that we need to set this property, and that we must find the best setting for our particular project.
Was this article helpful?