Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Accessing class variable across modules


Message #1 by "Neil Newton" <neil_n@h...> on Thu, 15 Nov 2001 01:17:06
Neil,

I use a global connection in one of my apps (don't worry, there aren't 
many concurrent users :¬) .  The connection is opened in one class and 
referenced in another class.  I've had no problems.  This is what I do. I 
must add that can't take credit for all of this code as most of was posted 
here by Stuart Naylor on 13 Jul 2001 (- thanks Stuart).

I use a connection class which exposes the Connect method to open 
mConnection and the public property (Get) Connection to share the 
connection with the rest of the app.  In basModule I call the Connect 
method (to open the connection). The GetRecords method in clsData 
references the Connection property and uses it to return a recordset.

<ClsConnnection>
Option Explicit

Private WithEvents mConnection As ADODB.Connection
Private mServerName As String
Private mDataBaseName As String
Private mUserName As String
Private mPassword As String

Public Property Let ServerName(ByVal newValue As String)
mServerName = newValue
End Property
Public Property Let DataBaseName(ByVal newValue As String)
mDataBaseName = newValue
End Property
Public Property Let UserName(ByVal newValue As String)
mUserName = newValue
End Property
Public Property Let Password(ByVal newValue As String)
mPassword = newValue
End Property
Public Property Get Connection() As ADODB.Connection
Set Connection = mConnection
End Property

Public Sub Connect()
Dim strConnection As String

Set mConnection = New ADODB.Connection

'Open connection etc...

strConnection = "Provider=SQLOLEDB.1;Initial Catalog=" & mDataBaseName & _
";Data Source=" & mServerName
mConnection.ConnectionTimeout = 7
mConnection.Open strConnection, mUserName, mPassword

End Sub

</ClsConnnection>

<basModule>
Option Explicit

Public Cnn As clsConnection

Sub Main()

Set Cnn = New clsConnection

Cnn.ServerName = GetSetting(App.Title, "Logon", "Server")
Cnn.DataBaseName = GetSetting(App.Title, "Logon", "Database")
Cnn.UserName = GetSetting(App.Title, "Logon", "Uid")
Cnn.Password = GetSetting(App.Title, "Logon", "Password")

Cnn.Connect
</basModule>

<ClsData>
Option Explicit

Public Sub GetRecords()

'instantiate module level command - this is the only place this occurs
Set mCMD = New ADODB.Command
'set command props
mCMD.ActiveConnection = Cnn.Connection

'Execute command, process data and all that good stuff...

End Sub
</ClsData>

HTH
andy

PS my apologies if there are some errors in this code - I have had to 
strip out a lot of other stuff for readability.

  Return to Index