468,115 Members | 1,945 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,115 developers. It's quick & easy.

Using a Class Module to Execute Code

twinnyfo
3,638 Expert Mod 2GB
Hello Friends,

'Tis me again, with what might possibly be a very simple question, but one I just don't know where to start searching. I will begin with an example that should make sense to everyone, then describe what I am trying to do.

First, a common example. Let us say we are working with an object. An object has properties, some of which can be changed, and it also has methods which do things. For example, a Form has the Property of BackColor, which can be changed do different colors. But Forms also have the Method of MoveSize, which does something: it moves the form or changes the visual dimensions of the form, using values for the Top, Left, Height and Width arguments for that method. In VBA it might look something like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ChangeForm()
  2.  
  3.     With Me
  4.         .BackColor = RGB(0, 0, 0)
  5.         Call .MoveSize(Top:=0, _
  6.                        Left:=0, _
  7.                        Height:=1296, _
  8.                        Width:=1800)
  9.     End With
  10.  
  11. End Sub
Super Simple!

Now, let's say, for example, that I have created a new class module that I have named Person:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Name      As String
  5. Public Height    As Integer 'Height in Inches for US folks
  6. Public HairColor As String
Then, when we want to create a person:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CreatePerson()
  2.     Dim perBlank As New Person
  3.  
  4.     With perBlank
  5.         .Name = "Hulk Hogan"
  6.         .Height = 80
  7.         .HairColor = "Blonde"
  8.     End With
  9.  
  10. End Sub
Thus, I can manipulate this data all day long. I can also use other code to display the data in this Person:

Expand|Select|Wrap|Line Numbers
  1. Private Sub DisplayPerson()
  2.  
  3.     Debug.Print perBlank.Name
  4.  
  5. End Sub
Which will result in this being displayed in the Immediate Window: Hulk Hogan.

So, finally, here is my question: Is there a way to add a Method to a Class, such that all I need to do is do this:

Expand|Select|Wrap|Line Numbers
  1. Call perBlank.Display
and that code will execute my Sub DisplayPerson?

Again, this may have a super simple answer--I just haven't been able to find any answers out there.

Thanks for the hepp!
2 Weeks Ago #1

✓ answered by NeoPa

Hi Twinny.

Yes indeed there is - and you're right that it's obvious once you know how.

Simply create a Public Sub or Public Function Procedure, depending on whether or not you need a value returned from your Method.

I actually have a Class example in my (fairly) recent Direct File I/O in VBA article that you can review if that helps.

In fact, looking at your example code, it seems you already have a Method called Person.CreatePerson() except you have it set as Private so it won't be visible to code outside of the Class Module itself.

3 3122
NeoPa
32,023 Expert Mod 16PB
Hi Twinny.

Yes indeed there is - and you're right that it's obvious once you know how.

Simply create a Public Sub or Public Function Procedure, depending on whether or not you need a value returned from your Method.

I actually have a Class example in my (fairly) recent Direct File I/O in VBA article that you can review if that helps.

In fact, looking at your example code, it seems you already have a Method called Person.CreatePerson() except you have it set as Private so it won't be visible to code outside of the Class Module itself.
2 Weeks Ago #2
twinnyfo
3,638 Expert Mod 2GB
NeoPa,

Ya know, I have been thinking about how one would accomplish this for weeks, then I decided to just submit a question--wrote out my thoughts and posted. Then I walked away for about 30 minutes and while I was away, I thought to myself, "Self, wouldn't one just declare a Public Sub in the Person Class Module?"

So, I came back and tried it, using my example, and it kind of worked, but I could not figure out how to send the "perBlank" object back to the Class Module, because that is where the code broke, so I just walked away again.

But, then your post just kick-started my brain: my perBlank object was not a public object. So, obviously, it wouldn't be able to find it. BUT, in my application, the object I've created IS public, so all I will have to do is have the code do what I need it to do!

So, this question is another example of 1) I feel really stupid about something so obvious, 2) It often helps to write your thoughts out and step away and 3) many solutions are often extremely simple, but one just needs to be reminded of something basic.

Thanks again for the hepp NeoPa!
2 Weeks Ago #3
NeoPa
32,023 Expert Mod 16PB
Don't feel stupid. You were clever enough to realise that, just like all of us have experienced, you were nearly there but unable to fit it all together quite right. Often the way with stuff we've never actually done before ;-)

I'm glad I was able to hepp in this case - even though I realised you were all there but for a hair's breadth.
2 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

42 posts views Thread by WindAndWaves | last post: by
4 posts views Thread by Steve Jorgensen | last post: by
6 posts views Thread by =?Utf-8?B?TmFt?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.