472,799 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 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 3857
NeoPa
32,534 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,534 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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.