Using an index with a class module?
I copy and pasted and type up some stuff so this is untested. But there
is enough here to get you started with class modules....
'Here's a class module ..... name it DBConnection:
'DatabaseConnectionInfo
Option Explicit
Dim propDatabase As String
Dim propServer As String
Dim propPort As String
Dim propPrjName As String
Dim propPassword As String
Public Property Get database() As String
database = propDatabase
End Property
Public Property Let database(newDatabase As String)
propDatabase = newDatabase
End Property
Public Property Get server() As String
server = propServer
End Property
Public Property Let server(newServer As String)
propServer = newServer
End Property
Public Property Get Port() As String
Port = propPort
End Property
Public Property Let Port(newPort As String)
propPort = newPort
End Property
Public Property Get userName() As String
userName = propPrjName
End Property
Public Property Let userName(newUserName As String)
propPrjName = newUserName
End Property
Public Property Get password() As String
password = propPassword
End Property
Public Property Let password(newPassword As String)
propPassword = newPassword
End Property
Example use:
Function GetConnectionInfo(book As Workbook, ID as Integer ) As
DBConnection
Dim db As New DBConnection
Dim ID, DATABASE_COL, SERVER_COL, PASSWORD_COL, USERNAME_COL
Dim sheet As Worksheet
Set sheet = book.Sheets("DBConfiguration")
DATABASE_COL = Utils.GetColumnNum(book.Sheets("DBConfiguration"),
"DATABASE")
SERVER_COL = Utils.GetColumnNum(book.Sheets("DBConfiguration"),
"SERVER")
PASSWORD_COL = Utils.GetColumnNum(book.Sheets("DBConfiguration"),
"PASSWORD")
USERNAME_COL = Utils.GetColumnNum(book.Sheets("DBConfiguration"),
"USERNAME")
db.database = sheet.Cells(ID, DATABASE_COL).Value
db.server = sheet.Cells(ID, SERVER_COL).Value
db.password = sheet.Cells(ID, PASSWORD_COL).Value
db.userName = sheet.Cells(ID, USERNAME_COL).Value
Set GetConnectionInfo = db
End Function
' Use in a Collection context:
Function DBConnectionCollection() as Collection
Dim dbCollection As Collection
Dim d1 as DBConnection
Dim d2 as DBConnection
Set d1 = Function GetConnectionInfo(ThisWorkbook, 1 )
Set d2 = Function GetConnectionInfo(ThisWorkbook, 2 )
dbCollection.Add d1
dbCollection.Add d2
Set DBConnectionCollection = dbCollection
End Function
Sub Test()
Dim dbCollections as Collection
Set dbCollection = DBConnectionCollection()
Dim d1 as DBConnection
Dim d2 as DBConnection
Set d1 = DBConnections(1)
Set d2 = dbCollections(2)
End Sub
|