tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,937
- Office Version
- 365
- 2019
- Platform
- Windows
Instead of having all this in a standard module:
it has been suggested to break it into two classes:
cData
and cExcelSetup
What I don't understand is why is this declared Private?
when all other subs are Public?
PS Don't say because you want it only accessible within the class!
I also noticed that when it is Private, I cannot write:
which surprises me because I thought as it's in the same class, it should be able to refer to itself with the keyword Me.
Thanks
Code:
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim xlSheet As Worksheet
Dim sConnString As String
Dim sSQL As String
Set xlSheet = Sheets("Sheet2")
xlSheet.Activate
Range("A1").Activate
Selection.CurrentRegion.Select
Selection.ClearContents
Range("A1").Select
Set cnn = New ADODB.Connection
sConnString = "Provider=SQLNCLI;Server=D2FZSC51\SQLEXPRESS;" _
& "Database=AdventureWorks;Trusted_Connection=yes;"
cnn.Open sConnString
sSQL = "SELECT HumanResources.Employee.EmployeeID, Person.Contact.FirstName," _
& " Person.Contact.LastName FROM Person.Contact" _
& " INNER JOIN HumanResources.Employee" _
& " ON Person.Contact.ContactID = HumanResources.Employee.ContactID" _
& " WHERE (((HumanResources.Employee.EmployeeID) In" _
& " (SELECT HumanResources.Employee.ManagerID" _
& " FROM HumanResources.Employee)));"
Set rs = New ADODB.Recordset
rs.Open sSQL, cnn, adOpenDynamic
Sheets("Sheet2").Activate
Range("A1").CopyFromRecordset rs
xlSheet.Select
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Columns.AutoFit
Range("A1").Select
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Set xlSheet = Nothing
it has been suggested to break it into two classes:
cData
Code:
Option Explicit
Private m_cnn As ADODB.Connection
Private m_rs As ADODB.Recordset
Private m_sConnString As String
Private m_sSQL As String
'
Public Property Get ConnectString() As String
ConnectString = m_sConnString
End Property
Public Property Let ConnectString(newString As String)
m_sConnString = newString
End Property
Public Property Get SQL() As String
SQL = m_sSQL
End Property
Public Property Let SQL(newSQL As String)
m_sSQL = newSQL
End Property
Function OpenConnection()
If m_sConnString <> "" Then
m_cnn.Open m_sConnString
Else
MsgBox "Cannot open connection", vbOKOnly, "cData: OpenConnection Error"
End If
End Function
Function CloseConnection()
m_cnn.Close
End Function
Function GetData() As ADODB.Recordset
m_rs.Open m_sSQL, m_cnn, adOpenDynamic
Set GetData = m_rs
End Function
Private Sub Class_Initialize()
m_sConnString = ""
m_sSQL = ""
Set m_cnn = New ADODB.Connection
Set m_rs = New ADODB.Recordset
End Sub
Private Sub Class_Terminate()
Set m_cnn = Nothing
Set m_rs = Nothing
End Sub
and cExcelSetup
Code:
Option Explicit
Private m_xlSheet As Worksheet
Private m_rngInitialCellSelect As Range
Private m_rngDataRegionStart As Range
'
Public Property Get Worksheet() As Worksheet
Set Worksheet = m_xlSheet
End Property
Public Property Set Worksheet(newSheet As Worksheet)
Set m_xlSheet = newSheet
End Property
Public Property Get InitialCellSelection() As Range
Set InitialCellSelection = m_rngInitialCellSelect
End Property
Public Property Set InitialCellSelection(newCell As Range)
Set m_rngInitialCellSelect = newCell
End Property
Public Property Get DataRegionStart() As Range
Set DataRegionStart = m_rngDataRegionStart
End Property
Public Property Set DataRegionStart(newCellAddress As Range)
Set m_rngDataRegionStart = newCellAddress
End Property
Public Sub SetKeyCells(InitialCell As Range, DataRegionStart As Range)
Set m_rngInitialCellSelect = InitialCell
Set m_rngDataRegionStart = DataRegionStart
End Sub
Public Sub SetupWorksheet()
Me.Worksheet.Activate
ClearRegion
Me.InitialCellSelection.Select
End Sub
Private Sub ClearRegion()
m_xlSheet.Activate
Me.DataRegionStart.Activate
Selection.CurrentRegion.Select
Selection.ClearContents
End Sub
Public Sub DoAutoFit()
Me.Worksheet.Select
Me.DataRegionStart.Select
Selection.CurrentRegion.Select
Selection.Columns.AutoFit
Me.InitialCellSelection.Select
End Sub
What I don't understand is why is this declared Private?
Code:
Private Sub ClearRegion()
m_xlSheet.Activate
Me.DataRegionStart.Activate
Selection.CurrentRegion.Select
Selection.ClearContents
End Sub
when all other subs are Public?
PS Don't say because you want it only accessible within the class!
I also noticed that when it is Private, I cannot write:
Code:
Me.ClearRegion
which surprises me because I thought as it's in the same class, it should be able to refer to itself with the keyword Me.
Thanks