HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Class Module to wrap up classic ADO call to SQL-server

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sqlmoduleclassicadocallserverclasswrap

Problem

I do a lot of copying excel vba code related to classic ADO from workbook to workbook, so I've decided to add the code to a Class Module in a single utility workbook and then in all other books just reference the utility book and use instances of the ADO wrapper class.

The class module is called clDataAccess

I would like to know any obvious flaws in the coding:

```
Option Explicit

'::::::::::::::::::::
'::
':: Class Variable Declarations
'::
'::::::::::::::::::::

Private mcnConnectString As String
Private mcnConnect As ADODB.Connection

'::::::::::::::::::::
'::
':: on initialization open an ADO connection through to the server
'::
'::::::::::::::::::::
Private Sub Class_Initialize()

mcnConnectString = _
"PROVIDER=SQLOLEDB.1;" & _
"PD=*****;" & _
"PERSIST SERITY INFO=**;" & _
"USER ID=;" & _
"INITIAL CATALOG=***;" & _
"DATA SOURCE=*;" & _
"USE PROCEDURE FOR PREPARE=*;" & _
"AUTO TRANSLATE=****;" & _
"CONNECT TIMEOUT=*;" & _
"COMMAND TIMEMOUT=*" & _
"PACKET SIZE=****;" & _
"TAG WITH COLUMN COLLATION WHEN POSSIBLE=****"

Call ensureClosedConn(mcnConnectString)

End Sub

'::::::::::::::::::::
'::
':: connection string property
':: user could need to change the connection
'::
'::::::::::::::::::::
Property Let connectionString(ByVal stConn As String)
mcnConnectString = connectionString
End Property

':::::::::::::::::
'::
':: This procedure attempts to create a connection to
':: the database using the specified connection string.
'::
'::::::::::::::::::::
Private Function ensureClosedConn(ByVal sConnection As String) As Boolean

On Error GoTo ErrorHandler

If (mcnConnect Is Nothing) Then
Set mcnConnect = New ADODB.Connection
With mcnConnect
.connectionString = sConnection
.CommandTimeout = 0
.Open
.Close
End With
Else
If (mcnConnect.S

Solution

Suggestions:

-
Don't assume the cursor type of the recordset, the user might want a different type.

-
Consider making a ConnectionString class that lets the user use properties to get and let the individual parameters, but allows assigning or returning a complete string

-
Don't force the connection to close, as the user might intend to make subsequent calls.

Context

StackExchange Code Review Q#116253, answer score: 7

Revisions (0)

No revisions yet.