473,386 Members | 1,779 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,386 software developers and data experts.

Global class instantiation in VBA?

patjones
931 Expert 512MB
Hi:

I'm fairly new to object-oriented programming, and am trying to implement a simple class in my VBA code in an attempt to move away from procedural based coding. Here's the set-up. I defined a class called cFormOps as follows:

Expand|Select|Wrap|Line Numbers
  1.  Option Compare Database
  2. Option Explicit 
  3.  
  4. Implements IFormOps
  5.  
  6. Private Sub IFormOps_ClearForm(frm As Form)
  7.  
  8. Dim ctl As Control
  9. For Each ctl In frm.Controls
  10.     If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
  11.         ctl.SetFocus
  12.         ctl.Text = ""
  13.     End If
  14. Next
  15.  
  16. End Sub
  17.  
And an implementation for it, IFormOps:

Expand|Select|Wrap|Line Numbers
  1.  Option Compare Database
  2. Option Explicit 
  3.  
  4. Public Sub ClearForm(frm As Form): End Sub
  5.  
I want to build on this class to perform some formatting tasks on various forms in my front end. So, in the form module I write out something like:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub cmdClear_Click() 
  2.  
  3. Dim cMyFormOps As cFormOps
  4. Dim iMyFormOps As IFormOps
  5.  
  6. Set cMyFormOps = New cFormOps
  7. Set iMyFormOps = cMyFormOps
  8.  
  9. iMyFormOps.ClearForm Me
  10.  
  11. End Sub
  12.  
My question is this. Is there a way for me to instantiate the class on a global level so that I don't have to do it in each form module where I want to use it?

In VB .NET, I know there's even a way to use class functionality without having to instantiate the class. Is that possible in VBA? Thanks.

Pat
May 16 '08 #1
6 5507
FishVal
2,653 Expert 2GB
Hello, Pat.

I think you have at least 3 options:
  • global variable in code module
  • local variable in a code module accessible via the module function
  • variable in MainForm module (if you have in your db everrunning form), accessible via MainForm property
But the way you are using class seems somewhat bizarre (for me at least). I don't see any advantage over regular public function. Really it has so generic functionality. ;)

Regards,
Fish
May 16 '08 #2
patjones
931 Expert 512MB
Hello, Pat.

I think you have at least 3 options:
  • global variable in code module
  • local variable in a code module accessible via the module function
  • variable in MainForm module (if you have in your db everrunning form), accessible via MainForm property
But the way you are using class seems somewhat bizarre (for me at least). I don't see any advantage over regular public function. Really it has so generic functionality. ;)

Regards,
Fish
I know, this class doesn't even have any properties. It essentially behaves like a global function. But it's really more academic for me than anything else at this point. Later on, I will be adding more substantial classes to this db to represent employees and leave of absence instances.

So, I chose to do this in my global code module:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public cMyFormOps As cFormOps
  3. Public iMyFormOps As IFormOps
  4.  
  5.  
And then this in the module that sits behind my main form, which is always open:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form_Open(Cancel As Integer) 
  2.  
  3. Set cMyFormOps = New cFormOps
  4. Set iMyFormOps = cMyFormOps
  5.  
  6. End Sub
  7.  
Thanks for your input...

Pat
May 16 '08 #3
FishVal
2,653 Expert 2GB
You are welcome.

Best regards,
Fish.
May 16 '08 #4
NeoPa
32,556 Expert Mod 16PB
It's a bit like a "Hello World" for OOP in VBA right Pat?

Just playing with it to get some hands-on experience and to get the concepts into your head :)
May 16 '08 #5
patjones
931 Expert 512MB
It's a bit like a "Hello World" for OOP in VBA right Pat?

Just playing with it to get some hands-on experience and to get the concepts into your head :)
Pretty much. I've found that writing out some common, simple tasks in OOP helps. The syntactical differences between VBA and .NET are a little annoying, but the concepts seem to be the same.

I guess there never comes a point where you stop learning! :-)
May 16 '08 #6
NeoPa
32,556 Expert Mod 16PB
Yes there is. It's about when they stop digging the 6-foot hole :D
May 16 '08 #7

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

Similar topics

7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
8
by: Mark Rae | last post by:
Hi, Another stupid newbie question from me, I'm sorry to say... but can anyone tell me how to simulate the concept of a global constant in a C# Windows app? The app in question contains...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
7
by: Carlos | last post by:
Ok, I am a VB developer but need to do a C# program, my question is how do I decalre a global variable in c#, in VB.net I juest create a procedure an declare my Variable as Public myVar and I can...
10
by: Simon Harvey | last post by:
Hi everyone, Can anyone tell me if I declare a global variable in my pages code behind, is it persisted if the page does a post back, or do I need to add the object to the session object in...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
4
by: Sheldon | last post by:
Hi, I have a series of classes that are all within the same file. Each is called at different times by the main script. Now I have discovered that I need several variables returned to the main...
3
by: Dean Roddey | last post by:
I just upgraded the compiler version we use in our product (we'd stuck with a very old one for a long time) and it seems that things have moved forward while we were off in the wilderness, and I'm...
23
by: Anil Gupte | last post by:
I am trying to set up a function that connects to the database that I can then use gloablly. I set up a class called L3Global in which I have a function as follows: Public Function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.