SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 104 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

Open m_sConnString
Else
MsgBox "Cannot open connection", vbOKOnly, "cData: OpenConnection Error"
End If
End Function
Function CloseConnection()
m_cnn.Close
End Function
Note that the OpenConnection method is using the private variable m_sConnString to
return the connection string to the AdventureWorks database.
Next we??™ll create a new function called GetData and add the following code:
Function GetData() As ADODB.Recordset
m_rs.Open m_sSQL, m_cnn, adOpenDynamic
Set GetData = m_rs
End Function
This function returns a dataset based on an SQL statement passed in from the private
variable m_sSQL, and uses the private connection object to connect to the database. In reality,
this is a very simplistic method. In the real world, we would probably add arguments or properties
for the cursor type, location, and other key settings, but for our example this will suffice.
CHAPTER 2 n DATA IN, DATA OUT 91
Our last order of business for this class is setting its initialization and termination
methods. It is good practice to initialize any internal objects and data variables, and the
Class_Initialize method is the place to do it. When using internal objects like the ADO
objects, using the Class_Terminate method allows us a place to clean them up when the
object goes out of scope in our client code.
Private Sub Class_Initialize()
m_sConnString = ""
m_sSQL = ""
Set m_cnn = New ADODB.Connection
Set m_rs = New ADODB.Recordset
Set m_prm = New ADODB.Parameter
Set m_cmd = New ADODB.


Pages:
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116