473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using a Class Module to Execute Code

twinnyfo
3,653 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!
Mar 29 '21 #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 3898
NeoPa
32,556 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.
Mar 29 '21 #2
twinnyfo
3,653 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!
Mar 29 '21 #3
NeoPa
32,556 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.
Mar 29 '21 #4

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

Similar topics

42
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
4
by: Steve Jorgensen | last post by:
In response to recent questions on when to use class modules in Access VBA, here's a real-world scenario of mine. I had some code that needed to build a Where clause for a Select statement based...
3
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
0
by: batakkeren | last post by:
Create new class module with Visual Basic 6.0, how can we call class itself??? script call/use those new classes.. New class module to connect database ACCESS, within class there's some...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
6
by: =?Utf-8?B?TmFt?= | last post by:
Using Response.Redirect(“error.htm”) in the catch block of a try-catch statement, I am redirecting a page to a generic page if an error occurs in code-behind of the page. Since...
5
topher23
by: topher23 | last post by:
I've seen a lot of questions about how to make secure database passwords. I'm going to go over a method of encrypting a password using the MD5 encryption algorithm for maximum security. First,...
7
NeoPa
by: NeoPa | last post by:
Overview This article shows how to set up and use a Class Module, as well as, more specifically, how to open forms such that they appear as a hierachical menu structure. Form A opens Form B and,...
4
Seth Schrock
by: Seth Schrock | last post by:
Thanks to TheSmileyCoder's Access Crash Reporter, I get an email when an error occurs in my database. Part of the information in the email is the name of the module/class module and the name of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.