D
David Blewett
Hi all, long-time lurker first-time poster. I've recently started
learning about classes in VBA. One thing that I don't understand is
why my class' private variables are visible in Object Explorer, or in
Locals when running a procedure. Isn't the point of an object to hide
these things from a developer? Here are my class definitions, maybe
I'm declaring things wrong. All 3 objects' module level private
variables used to store the actual data are visible.
David Blewett
Top-Level Container:
Option Compare Database
Option Explicit
Private Const cBUPath As String = "C:\Documents and
Settings\xdeb133\My Documents\BU\"
Private dteSetID As Date
Private strDestPath As String
Private fles As FraudDetection_fe.Files
Private Sub Class_Initialize()
Set fles = New FraudDetection_fe.Files
dteSetID = Now
strDestPath = cBUPath
Fill
End Sub
Public Property Get SetID() As Date
SetID = dteSetID
End Property
Public Property Let SetID(ByVal vNewValue As Date)
SetID = vNewValue
End Property
'Public Property Get File(Key As Variant) As FraudDetection_fe.File
'Set File = fles(Key)
'End Property
Public Function Add(Path As String, Name As String, Optional Checked
As Boolean = False) As FraudDetection_fe.File
Set Add = fles.Add(Path, Name, Checked)
End Function
Private Sub Class_Terminate()
Set fles = Nothing
End Sub
Public Function Delete(Key As String) As Boolean
fles.Delete Key
End Function
Public Property Get Files(Optional Key As Variant) As Object
If IsMissing(Key) = True Then
Set Files = fles
Else
Set Files = fles(Key)
End If
End Property
Public Property Get Destination() As String
Destination = strDestPath
End Property
Public Property Let Destination(ByVal vNewValue As String)
strDestPath = vNewValue
End Property
Public Function Fill()
Dim rsFiles As DAO.Recordset
Dim strQuery As String
If OpenConn = True Then
strQuery = "qselFiles"
Set rsFiles = DB.OpenRecordset(strQuery, dbOpenSnapshot)
Do While rsFiles.EOF = False
Add rsFiles.Fields("Path").Value,
rsFiles.Fields("FileName").Value
rsFiles.MoveNext
Loop
rsFiles.Close
Set rsFiles = Nothing
End If
End Function
Files Collection:
Option Compare Database
Option Explicit
Private colFiles As Collection
Private Sub Class_Initialize()
Set colFiles = New Collection
End Sub
Public Property Get Count() As Variant
Count = colFiles.Count
End Property
Public Function Add(ByVal Path As String, ByVal Name As String, _
Optional ByVal Checked As Boolean) As
FraudDetection_fe.File
Dim fle As FraudDetection_fe.File
Dim strMsg As String, strKey As String
Set fle = New FraudDetection_fe.File
strKey = NewID
fle.FileID = strKey
fle.Path = Path
fle.Name = Name
fle.Checked = Checked
colFiles.Add fle, strKey
Set Add = fle
Set fle = Nothing
Exit_Add:
Exit Function
Err_Add:
MsgBox strMsg
GoTo Exit_Add
End Function
Public Function Item(Key As Variant) As FraudDetection_fe.File
Set Item = colFiles(Key)
End Function
Private Sub Class_Terminate()
Set colFiles = Nothing
End Sub
Public Function Delete(Key As String) As Boolean
colFiles.Remove Key
End Function
Private Function NewID() As String
Static intFileNum As Integer
intFileNum = intFileNum + 1
NewID = "File" & Format(intFileNum, "0000000")
End Function
Public Property Get NewEnum() As IUnknown
Set NewEnum = colFiles.[_NewEnum]
End Property
File Object:
Option Compare Database
Option Explicit
Private strFileID As String
Private blnChecked As Boolean
Private strPath As String
Private strName As String
Private strDrive As String
Private strParentDir As String
Private blnExists As Boolean
Private Sub Class_Initialize()
Exit_Initialize:
Exit Sub
Err_Initialize:
MsgBox "The Path must be an array."
GoTo Exit_Initialize
End Sub
Public Property Get FileID() As String
FileID = strFileID
End Property
Public Property Let FileID(ByVal vNewValue As String)
strFileID = vNewValue
End Property
Public Property Get Checked() As Boolean
Checked = blnChecked
End Property
Public Property Let Checked(ByVal vNewValue As Boolean)
blnChecked = vNewValue
End Property
Public Property Get Path() As String
Path = strPath
End Property
Public Property Let Path(ByVal vNewValue As String)
Dim strTemp() As String
strTemp = ParsePath(vNewValue)
strDrive = strTemp(0)
strParentDir = strTemp(UBound(strTemp))
strPath = vNewValue
End Property
Public Property Get Name() As String
Name = strName
End Property
Public Property Let Name(ByVal vNewValue As String)
strName = vNewValue
End Property
Public Property Get Drive() As String
Drive = strDrive
End Property
Public Property Let Drive(ByVal vNewValue As String)
strDrive = vNewValue
End Property
Public Property Get ParentDir() As String
ParentDir = strParentDir
End Property
Public Property Let ParentDir(ByVal vNewValue As String)
strParentDir = vNewValue
End Property
Private Function ParsePath(Path As String, Optional IncludeColon As
Boolean = False, Optional LastDirOnly As Boolean = False) As Variant
'Function returns a string array containing each directory in a given
path
Dim strDrive As String, strWorkingPath As String, strPath() As String
Dim intBuffer As Integer, intLoop As Integer
If Left(Path, 1) = "\" Then
If Right(Path, 1) = "\" Then
intBuffer = 1
Else
intBuffer = 0
End If
'If ExplorerFormat = True Then
' strDrive = Left(Path, InStr(3, Path, "\") - 3)
' Else
strDrive = Mid(Path, 3, InStr(3, Path, "\") - 3)
'End If
strWorkingPath = Mid(Path, InStr(3, Path, "\") + 1, Len(Path)
- InStr(3, Path, "\") - intBuffer)
Else
If Right(Path, 1) = "\" Then
intBuffer = 4
Else
intBuffer = 3
End If
If IncludeColon = True Then
strDrive = Left(Path, 2)
Else
strDrive = Left(Path, 1)
End If
strWorkingPath = Mid(Path, 4, Len(Path) - intBuffer)
End If
strPath = Split(strWorkingPath, "\")
ReDim Preserve strPath(UBound(strPath) + 1) As String
For intLoop = UBound(strPath) To 0 Step -1
If intLoop > 0 Then
strPath(intLoop) = strPath(intLoop - 1)
Else
strPath(intLoop) = strDrive
End If
Next intLoop
If LastDirOnly = True Then
ParsePath = strPath(UBound(strPath))
Else
ParsePath = strPath
End If
End Function
learning about classes in VBA. One thing that I don't understand is
why my class' private variables are visible in Object Explorer, or in
Locals when running a procedure. Isn't the point of an object to hide
these things from a developer? Here are my class definitions, maybe
I'm declaring things wrong. All 3 objects' module level private
variables used to store the actual data are visible.
David Blewett
Top-Level Container:
Option Compare Database
Option Explicit
Private Const cBUPath As String = "C:\Documents and
Settings\xdeb133\My Documents\BU\"
Private dteSetID As Date
Private strDestPath As String
Private fles As FraudDetection_fe.Files
Private Sub Class_Initialize()
Set fles = New FraudDetection_fe.Files
dteSetID = Now
strDestPath = cBUPath
Fill
End Sub
Public Property Get SetID() As Date
SetID = dteSetID
End Property
Public Property Let SetID(ByVal vNewValue As Date)
SetID = vNewValue
End Property
'Public Property Get File(Key As Variant) As FraudDetection_fe.File
'Set File = fles(Key)
'End Property
Public Function Add(Path As String, Name As String, Optional Checked
As Boolean = False) As FraudDetection_fe.File
Set Add = fles.Add(Path, Name, Checked)
End Function
Private Sub Class_Terminate()
Set fles = Nothing
End Sub
Public Function Delete(Key As String) As Boolean
fles.Delete Key
End Function
Public Property Get Files(Optional Key As Variant) As Object
If IsMissing(Key) = True Then
Set Files = fles
Else
Set Files = fles(Key)
End If
End Property
Public Property Get Destination() As String
Destination = strDestPath
End Property
Public Property Let Destination(ByVal vNewValue As String)
strDestPath = vNewValue
End Property
Public Function Fill()
Dim rsFiles As DAO.Recordset
Dim strQuery As String
If OpenConn = True Then
strQuery = "qselFiles"
Set rsFiles = DB.OpenRecordset(strQuery, dbOpenSnapshot)
Do While rsFiles.EOF = False
Add rsFiles.Fields("Path").Value,
rsFiles.Fields("FileName").Value
rsFiles.MoveNext
Loop
rsFiles.Close
Set rsFiles = Nothing
End If
End Function
Files Collection:
Option Compare Database
Option Explicit
Private colFiles As Collection
Private Sub Class_Initialize()
Set colFiles = New Collection
End Sub
Public Property Get Count() As Variant
Count = colFiles.Count
End Property
Public Function Add(ByVal Path As String, ByVal Name As String, _
Optional ByVal Checked As Boolean) As
FraudDetection_fe.File
Dim fle As FraudDetection_fe.File
Dim strMsg As String, strKey As String
Set fle = New FraudDetection_fe.File
strKey = NewID
fle.FileID = strKey
fle.Path = Path
fle.Name = Name
fle.Checked = Checked
colFiles.Add fle, strKey
Set Add = fle
Set fle = Nothing
Exit_Add:
Exit Function
Err_Add:
MsgBox strMsg
GoTo Exit_Add
End Function
Public Function Item(Key As Variant) As FraudDetection_fe.File
Set Item = colFiles(Key)
End Function
Private Sub Class_Terminate()
Set colFiles = Nothing
End Sub
Public Function Delete(Key As String) As Boolean
colFiles.Remove Key
End Function
Private Function NewID() As String
Static intFileNum As Integer
intFileNum = intFileNum + 1
NewID = "File" & Format(intFileNum, "0000000")
End Function
Public Property Get NewEnum() As IUnknown
Set NewEnum = colFiles.[_NewEnum]
End Property
File Object:
Option Compare Database
Option Explicit
Private strFileID As String
Private blnChecked As Boolean
Private strPath As String
Private strName As String
Private strDrive As String
Private strParentDir As String
Private blnExists As Boolean
Private Sub Class_Initialize()
Exit_Initialize:
Exit Sub
Err_Initialize:
MsgBox "The Path must be an array."
GoTo Exit_Initialize
End Sub
Public Property Get FileID() As String
FileID = strFileID
End Property
Public Property Let FileID(ByVal vNewValue As String)
strFileID = vNewValue
End Property
Public Property Get Checked() As Boolean
Checked = blnChecked
End Property
Public Property Let Checked(ByVal vNewValue As Boolean)
blnChecked = vNewValue
End Property
Public Property Get Path() As String
Path = strPath
End Property
Public Property Let Path(ByVal vNewValue As String)
Dim strTemp() As String
strTemp = ParsePath(vNewValue)
strDrive = strTemp(0)
strParentDir = strTemp(UBound(strTemp))
strPath = vNewValue
End Property
Public Property Get Name() As String
Name = strName
End Property
Public Property Let Name(ByVal vNewValue As String)
strName = vNewValue
End Property
Public Property Get Drive() As String
Drive = strDrive
End Property
Public Property Let Drive(ByVal vNewValue As String)
strDrive = vNewValue
End Property
Public Property Get ParentDir() As String
ParentDir = strParentDir
End Property
Public Property Let ParentDir(ByVal vNewValue As String)
strParentDir = vNewValue
End Property
Private Function ParsePath(Path As String, Optional IncludeColon As
Boolean = False, Optional LastDirOnly As Boolean = False) As Variant
'Function returns a string array containing each directory in a given
path
Dim strDrive As String, strWorkingPath As String, strPath() As String
Dim intBuffer As Integer, intLoop As Integer
If Left(Path, 1) = "\" Then
If Right(Path, 1) = "\" Then
intBuffer = 1
Else
intBuffer = 0
End If
'If ExplorerFormat = True Then
' strDrive = Left(Path, InStr(3, Path, "\") - 3)
' Else
strDrive = Mid(Path, 3, InStr(3, Path, "\") - 3)
'End If
strWorkingPath = Mid(Path, InStr(3, Path, "\") + 1, Len(Path)
- InStr(3, Path, "\") - intBuffer)
Else
If Right(Path, 1) = "\" Then
intBuffer = 4
Else
intBuffer = 3
End If
If IncludeColon = True Then
strDrive = Left(Path, 2)
Else
strDrive = Left(Path, 1)
End If
strWorkingPath = Mid(Path, 4, Len(Path) - intBuffer)
End If
strPath = Split(strWorkingPath, "\")
ReDim Preserve strPath(UBound(strPath) + 1) As String
For intLoop = UBound(strPath) To 0 Step -1
If intLoop > 0 Then
strPath(intLoop) = strPath(intLoop - 1)
Else
strPath(intLoop) = strDrive
End If
Next intLoop
If LastDirOnly = True Then
ParsePath = strPath(UBound(strPath))
Else
ParsePath = strPath
End If
End Function