Hey Group,
Wanted to know if anyone has been able code Classes in VBA where you can create a structured group of items for example
PETS.DOGS.NAME
and how would you code that in VBA.
Thanks,
Joe P.
15 12520
Hi, Joe.
In class PETS declare property DOGS returning object of class DOGS. Class module PETS -
Dim objDogs As New DOGS
-
-
Public Property Get DOGS() As DOGS
-
Set DOGS = objDogs
-
End Property
-
-
Public Property Set DOGS(ByVal objNewValue As DOGS)
-
Set objDogs = objNewValue
-
End Property
-
Class module DOGS -
Dim strName As String
-
-
Public Property Get Name() As String
-
Name = strName
-
End Property
-
-
Public Property Let Name(ByVal strNewValue As String)
-
strName = strNewValue
-
End Property
-
Now an example in some code module -
Public Sub SomeSub()
-
Dim objPets As New PETS
-
With objPets.DOGS
-
.Name = "qqq"
-
Debug.Print .Name
-
End With
-
Set objPets = Nothing
-
End Sub
-
Regards,
Fish
Thanks a million, that worked perfectly. I've been searching for that for months.
Thanks again,
Joe P.
Hi, Joe.
In class PETS declare property DOGS returning object of class DOGS. Class module PETS -
Dim objDogs As New DOGS
-
-
Public Property Get DOGS() As DOGS
-
Set DOGS = objDogs
-
End Property
-
-
Public Property Set DOGS(ByVal objNewValue As DOGS)
-
Set objDogs = objNewValue
-
End Property
-
Class module DOGS -
Dim strName As String
-
-
Public Property Get Name() As String
-
Name = strName
-
End Property
-
-
Public Property Let Name(ByVal strNewValue As String)
-
strName = strNewValue
-
End Property
-
Now an example in some code module -
Public Sub SomeSub()
-
Dim objPets As New PETS
-
With objPets.DOGS
-
.Name = "qqq"
-
Debug.Print .Name
-
End With
-
Set objPets = Nothing
-
End Sub
-
Regards,
Fish
Really going to the DOGS, hey FishVal. (LOL).
Thanks a million, that worked perfectly. I've been searching for that for months.
Thanks again,
Joe P.
Not a problem, Joe.
Good luck.
Really going to the DOGS, hey FishVal. (LOL).
Just dogging it the whole time. (LOL)
Kind regards,
Fish
NeoPa 32,556
Expert Mod 16PB
Just dogging it the whole time. (LOL)
I HOPE that means something different over there than it does over here in Blighty :D
PS. Don't even ask!!
I HOPE that means something different over there than it does over here in Blighty :D
PS. Don't even ask!!
LOL ???.
Honestly, I've meant that I was writing this while I was supposed to do some other important job (so what?). Does that mean something substantially else over there in Blighty? Anyway you've quoted it as bold and underlined, so it isn't a bad word itself. ;) I've got it from ABBYY Lingvo.
Means different things on different sides of the big pond :-)
Great work on the class example, Fish!
Regards,
Scott
NeoPa 32,556
Expert Mod 16PB
I wasn't criticising Fish. Far from it. Just having a laugh at the wording (which is a little less innocent here than it probably is for you) :D
I wasn't criticising Fish. Far from it. Just having a laugh at the wording (which is a little less innocent here than it probably is for you) :D
And I didn't take it as criticism. :P
I've referred with ABBYY Lingvo again. Really deserves LOL. I've used American slang meaning while British slang meaning is completely other. :D
The funny thing here is that yesterday I had a discussion with my boss concerning differences in English spoken in different countries (or even regions). Now it less seems me he was exaggerating. LOL.
Kind regards.
Fish
@ADezii
Hello all,
I have one more question in regards to the Class Collection, I'm trying to include custom events within the classes. What would be my best approach?
Since I've already tried to implement them, the problem that I'm having is I'm not able to use the NEW option when defining the variable that are going to hold the objects for the sub-classes. Example: -
'Old Statement
-
-
Dim Techs as new clsTechsV2
-
Dim Teams as new clsTeamsV2
-
-
-
'Added withevents
-
Dim withevents Techs as clsTechsV2
-
Dim withevents Teams as clsTeamV2
-
-
Since I'm getting the error message "Runtime Error 91 - Object variable, or With block not set"?
Is there a way around this, or some kind of remedy.
Below is a code snippet:
Class Name BatchClass -
Dim WithEvents Techs As clsTechV2
-
Dim WithEvents Teams As clsTeamV2
-
-
Public Property Get Tech() As clsTechV2
-
Set Tech = Techs
-
End Property
-
Public Property Get Team() As clsTeamV2
-
Set Team = Teams
-
End Property
-
Class Name "clsTechV2" -
Public Event TechError(ErrNumber As Integer, ErrDescription As String)
-
-
Public Function GetTechInfo() As Boolean
-
Set cn = New ADODB.Connection
-
Set cmd = New ADODB.Command
-
Dim rs As ADODB.Recordset
-
Set rs = New ADODB.Recordset
-
Dim DestroyedRS As ADODB.Recordset
-
Dim DestroyedBLV As ADODB.Recordset
-
Dim FNVPending As ADODB.Recordset
-
Dim BLVHours As ADODB.Recordset
-
Dim TechSchedule As ADODB.Recordset
-
Dim FNVCurrentPending As ADODB.Recordset
-
Dim TechProcImages As ADODB.Recordset
-
Dim TodaysCounts As ADODB.Recordset
-
Dim TodaysBLVHours As ADODB.Recordset
-
-
Dim strSQL As String
-
On Error GoTo Err_GetTechInfo
-
If Len(in_ScanTechName) < 1 Then
-
'MsgBox "Must set ScanTechName, StartDate, EndDate, and Location before calling GetTechInfo.", vbCritical + vbOKOnly
-
RaiseEvent TechError(1, "Must set ScanTechName Property before calling GetTechInfo.")
-
GetTechInfo = False
-
Exit Function
-
End If
-
If in_StartDate < 1 Then
-
'MsgBox "Must Define valid date value in StartDate before calling GetTechInfo.", vbCritical + vbOKOnly
-
RaiseEvent TechError(2, "Must set StartDate Property before calling GetTechInfo Method.")
-
GetTechInfo = False
-
Exit Function
-
End If
-
If in_EndDate < 1 Then
-
'MsgBox "Must Define valid date value in EndDate before calling GetTechInfo.", vbCritical + vbOKOnly
-
RaiseEvent TechError(3, "Must set EndDate Property before calling GetTechInfo Method.")
-
GetTechInfo = False
-
Exit Function
-
End If
-
If Len(in_Location) < 1 Then
-
'MsgBox "Must set Location value before calling GetTechInfo.", vbCritical + vbOKOnly
-
RaiseEvent TechError(4, "Must set Location property before calling GetTechInfo Method.")
-
GetTechInfo = False
-
Exit Function
-
End If
-
If Len(in_FNVRate) < 1 Then
-
'MsgBox "Must Define FNVRate Variable.", vbCritical + vbOKOnly
-
RaiseEvent TechError(5, "Must set FNVRate property before calling GetTechInfo Method.")
-
GetTechInfo = False
-
Exit Function
-
End If
-
If Len(in_ProductionRate) < 1 Then
-
'MsgBox "Must define ProductionRate Variable.", vbCritical + vbOKOnly
-
RaiseEvent TechError(6, "Must set ProductionRate property before calling GetTechInfo Method.")
-
GetTechInfo = False
-
Exit Function
-
End If
-
Class Name "clsTeamV2" -
Public Event TeamError(ErrNumber As Integer, ErrDescription As String)
-
-
Public Function GetTeamStats() As Boolean
-
Dim rs As ADODB.Recordset
-
Dim DestroyedRS As ADODB.Recordset
-
Dim BLVHours As ADODB.Recordset
-
Dim ProdHoursRS As ADODB.Recordset
-
Dim NonProdHoursRS As ADODB.Recordset
-
Dim TeamProdHoursRS As ADODB.Recordset
-
Dim TeamNonProdHoursRS As ADODB.Recordset
-
Dim TeamSchedule As ADODB.Recordset
-
Dim TeamLineOfBusiness As ADODB.Recordset
-
Dim TeamCosts As ADODB.Recordset
-
Dim TeamProcImages As ADODB.Recordset
-
Dim TeamCount As ADODB.Recordset
-
Dim TeamProdDays As ADODB.Recordset
-
Dim strSQL As String
-
Set cmd = New ADODB.Command
-
Set cnx = New ADODB.Connection
-
With cnx
-
.Provider = "MSDAORA"
-
.Properties("Data Source").Value = "EU"
-
.Properties("User ID").Value = "PRESUSER"
-
.Properties("Password").Value = "WASHINGTON"
-
.Open
-
End With
-
'Set ec = New clsErrorChecks
-
'Error Checking to make sure that Team Name and Start and End Dates are set
-
If IsNull(tm_TeamName) Or Len(tm_TeamName) < 1 Then
-
'MsgBox "Must Set Team Name Before Calling GetTeamStats Function.", vbCritical + vbOKOnly
-
RaiseEvent TeamError(1, "TeamName Value not Defined. Must set TeamName property before calling GetTeamStats Method.")
-
GetTeamStats = False
-
Exit Function
-
End If
-
If tm_StartDate <= 1 Then
-
'MsgBox "Must Set a valid Starting Date before calling GetTeamStats Function.", vbCritical + vbOKOnly
-
RaiseEvent TeamError(2, "StartDate Value not Defined. Must set StartDate Property before calling GetTeamStats Method.")
-
GetTeamStats = False
-
Exit Function
-
End If
-
If tm_EndDate <= 1 Then
-
'MsgBox "Must Set a valid Ending Date before calling GetTeamStats Function.", vbCritical + vbOKOnly
-
RaiseEvent TeamError(3, "EndDate property not defined. Must set EndDate Property before calling GetTeamStats Method.")
-
GetTeamStats = False
-
Exit Function
-
End If
-
Thanks for any help someone could provide.
Joe P.
Hello, Joe.
Really, object variable could not be declared using New with events to get it instantiated automatically when needed.
You need to instantiate it manually via
Set objDeclaredWithEvents = New clsClassRaisingEvent
before first use.
Teachs and Teams could be suitably instantiated in code handling Initialize event of BatchClass class. -
Private Sub Class_Initialize()
-
Set Techs = New clsTechV2
-
Set Teams = New clsTeamV2
-
End Sub
-
Regards,
Fish
Thanks for the reply FishVal,
Joe P.
You are welcome Joe.
BTW, IMHO it is not a case to use events. Raising error would be more suitable. See example below.
Class module: clsChild -
Public Function Foo(intInput As Long) As Long
-
-
Select Case intInput
-
Case 0
-
Foo = 0
-
Case 1
-
Err.Raise Number:=10000, Source:="clsChild.Foo", Description:="Value could not be 1"
-
Case 2
-
Err.Raise Number:=10001, Source:="clsChild.Foo", Description:="Value could not be 2"
-
Case 3
-
Err.Raise Number:=10002, Source:="clsChild.Foo", Description:="Value could not be 3"
-
Case 4
-
Err.Raise Number:=10003, Source:="clsChild.Foo", Description:="Value could not be 4"
-
Case Else
-
Err.Raise Number:=10100, Source:="clsChild.Foo", Description:="Unspecified error"
-
End Select
-
-
End Function
-
Class module: clsParent -
Private objChild As New clsChild
-
-
Public Sub Foo()
-
-
Dim i As Long
-
-
On Error GoTo ErrHandler
-
-
For i = 0 To 10
-
Debug.Print objChild.Foo(i)
-
Next i
-
-
Exit Sub
-
-
ErrHandler:
-
Debug.Print Err.Number, Err.Source, Err.Description
-
Resume Next
-
-
End Sub
-
@FishVal
Thanks again for the tip FishVal. I'll see how I can incorporate that into my current design.
Joe P.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul Morrow |
last post by:
I'm hoping that someone can explain why I get the following exception.
When I execute the code...
######################################
class Parent(object):
class Foo(object):
baz = 'hello...
|
by: Erik Bongers |
last post by:
Hi,
Nested classes only seem to be able to access static members of the
surrounding class :
class SurroundingClass
{
public:
class InnerClass
{
public:
|
by: Vinodh Kumar P |
last post by:
I abstract a Car, and the parts wihtin a car, with a C++ class.
Since only my version of class Car uses the classes defined for its parts,
is it a good practice to do like this?
class Car
{...
|
by: Terry E Dow |
last post by:
Howdy,
I am having trouble with the objectCategory=group member.Count attribute.
I get one of three counts, a number between 1-999, no member (does not
contain member property), or 0. Using...
|
by: Gerrit Beuze |
last post by:
Hi all,
I'm wondering if you how you organize as (in sorting / order) your C# class code:
Do you sort/ group by member type: fields, methods, properties etc.?
If yes: what ordering scheme do...
|
by: Robert W. |
last post by:
I've almost completed building a Model-View-Controller but have run into a
snag. When an event is fired on a form control I want to automatically
updated the "connnected" property in the Model. ...
|
by: Bob Day |
last post by:
Using VS2003, VB.NET, MSDE...
I am looking at a demo program that, to my surprise, has nested classes,
such as the example below. I guess it surprised me becuase you cannot have
nested subs,...
|
by: Cousson, Benoit |
last post by:
Defining it as a nested class saves you one line
The whole point of nested class is to avoid polluting the namespace with classes that are only used locally. So the argument about the elegance of...
|
by: Maric Michaud |
last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit :
I was not aware of any "nested classes are unsupported" before and didn't
consider nested classes as bad practice till...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |