Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 12-07-2017, 06:30 AM   #1
chrisjames25
Newly Registered User
 
Join Date: Dec 2014
Posts: 182
Thanks: 3
Thanked 0 Times in 0 Posts
chrisjames25 is on a distinguished road
Declaring adn defininf public variables

Hi

NIce and simple one for the forum i think.

If i want to declare some variables that i can then reference within the whole vba code for a particular form how do i do it?

At moment i am doing

Code:
Public StrTier1, StrTier11  As String
StrTier1 = "Category"
StrTier11 = "category"
However it is not defining the variable for me.

I get very confused with naming conventions, but i want these to be available for the whole of a form but not be used outside the form so do they need to be public or can then be private. If private how do i define them so not associated with only one event of the form.

chrisjames25 is offline   Reply With Quote
Old 12-07-2017, 06:40 AM   #2
jdraw
Super Moderator
 
jdraw's Avatar
 
Join Date: Jan 2006
Location: Ottawa, Ontario, Canada;West Palm Beach, Florida
Posts: 10,861
Thanks: 41
Thanked 1,735 Times in 1,686 Posts
jdraw is just really nice jdraw is just really nice jdraw is just really nice jdraw is just really nice
Re: Declaring adn defininf public variables

In plain English, using terms you would use when taking to a 6 year old, please describe to readers what you are trying to accomplish. No jargon, no quasi database terms.

In Access you must explicitly dim variables:

Dim str1 as String
Dim str2 as String
or
Dim str1 as string, str2 as string

Some info on Global, Public, Private

More info
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
jdraw is offline   Reply With Quote
Old 12-07-2017, 07:10 AM   #3
jleach
Newly Registered User
 
Join Date: Jan 2012
Location: New York, New York
Posts: 117
Thanks: 6
Thanked 23 Times in 23 Posts
jleach will become famous soon enough
Re: Declaring adn defininf public variables

In VBA there's (essentially) three different variable scopes that can be used:

1) Procedure level: this is the most common, where the variable is defined (dimmed) within the procedure, and is not visible outside the procedure
2) "Private" or "Module" level scope, which is where the variable is declared at the top of the module and is visible to all code within that module, but not outside the module. This is done via "Private x As Long" before any procedures within a module.
3) "Public" scope, which is available to all code within the module, as well as all code outside of the module (more or less... close enough of gov't work). This is done by declaring a module as "Public" or "Global" at the module level (ie, before any procedures in the module).

Code:
Option Compare Database
Option Explicit

' available to all code
Public MyPublicVariable As String

' available to all code in this module
Private MyModuleLevelVariable As String

'start of procedures/end of module declaration area
Private Function DoStuff()
  Dim x As Long  'only available to this function
  ...
End Function
(public and private functions and subs similarly dictate whether they can be called from within or without their containing modules, but aren't directly related to the variables).

Some general reading on scope and lifetime is recommended. Chip Pearson (linked by jdraw) has one of the best VBA reference sites in existence, but sometimes can be a bit much for a beginner. In any case, googling "vba scope and lifetime" should land you plenty where you can find something at your level.

Cheers,

__________________
- Jack D. Leach - Dymeng Services
jleach is online now   Reply With Quote
Old 12-07-2017, 07:49 AM   #4
jdraw
Super Moderator
 
jdraw's Avatar
 
Join Date: Jan 2006
Location: Ottawa, Ontario, Canada;West Palm Beach, Florida
Posts: 10,861
Thanks: 41
Thanked 1,735 Times in 1,686 Posts
jdraw is just really nice jdraw is just really nice jdraw is just really nice jdraw is just really nice
Re: Declaring adn defininf public variables

Further to info so far, you may find this free youtube video by Steve Bishop helpful.

Good luck.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
jdraw is offline   Reply With Quote
Old 12-07-2017, 12:55 PM   #5
chrisjames25
Newly Registered User
 
Join Date: Dec 2014
Posts: 182
Thanks: 3
Thanked 0 Times in 0 Posts
chrisjames25 is on a distinguished road
Re: Declaring adn defininf public variables

Thank you both for your help. It was private level scope i was after. I kept not getting the string to populate as expected because it was dimmed at procedure level.

I will look into all the resources as think an understanding of this will be crucial going forwards.
chrisjames25 is offline   Reply With Quote
Old 12-07-2017, 01:10 PM   #6
The_Doc_Man
AWF VIP
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 10,876
Thanks: 38
Thanked 886 Times in 798 Posts
The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all
Re: Declaring adn defininf public variables

Quote:
Public StrTier1, StrTier11 As String
Two things are wrong with what you posted vs. what you later said you needed.

1. Syntax: This should be

Code:
Public StrTier1 As String, StrTier11  As String
As you had it written, StrTier1 was a Variant. You can STILL assign a string value to it and it will work just fine - but it is VERY SLIGHTLY less efficient if your intent is that it would ALWAYS be a string and NEVER anything else. I suspect that the difference would only be detectable by timing two loops, each with an iteration count of 1,000,000 or so and each using one of the two definitions.

2. Semantics: Make it

Code:
Private StrTier1 As String, StrTier11  As String
(Or instead of Private you could use Dim, since Private is the default, I think.)

And as you have already acknowledged, this declaration must be in the declaration area of the module to be visible to the rest of the module.
__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over six months and survived being home all day with the wife. She must really love me.
The_Doc_Man is offline   Reply With Quote
Old 12-08-2017, 04:11 AM   #7
Uncle Gizmo
Nifty Access Guy
 
Uncle Gizmo's Avatar
 
Join Date: Jul 2003
Location: Newbury Berks UK
Posts: 8,958
Thanks: 295
Thanked 692 Times in 660 Posts
Uncle Gizmo has a spectacular aura about Uncle Gizmo has a spectacular aura about Uncle Gizmo has a spectacular aura about
Send a message via Skype™ to Uncle Gizmo
Re: Declaring adn defininf public variables

Quote:
Originally Posted by The_Doc_Man View Post
Code:
Private StrTier1 As String, StrTier11  As String
(Or instead of Private you could use Dim, since Private is the default, I think.)
So you can write:-
Code:
Private StrTier1 As String
instead of:-
Code:
Dim StrTier1 As String

I didn't know that! I will give that a try when I writing my next piece of code...

In one way it's clearer and in another it's a bit confusing? Not sure about it really...

__________________
Tony Hine - My Aspiration --- Finding a better way of Explaining ---
From this YouTube:-

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Uncle Gizmo is offline   Reply With Quote
Old 12-08-2017, 05:57 AM   #8
The_Doc_Man
AWF VIP
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 10,876
Thanks: 38
Thanked 886 Times in 798 Posts
The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all
Re: Declaring adn defininf public variables

"Dim" is shorter, but for a class module it is all you need in the declaration area. In ANY area, actually, since "Public" in a class module is close to totally meaningless. Just for snorts and giggles look up the "Friend" declaration, which is a strange little category that isn't quite public but isn't quite private either.

Actually, the only time I ever used "Private" was when I built event code and the wizard declared the entry point that way.
__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over six months and survived being home all day with the wife. She must really love me.
The_Doc_Man is offline   Reply With Quote
Old 12-08-2017, 08:55 AM   #9
Mark_
Newly Registered User
 
Join Date: Sep 2017
Posts: 363
Thanks: 5
Thanked 63 Times in 62 Posts
Mark_ is on a distinguished road
Re: Declaring adn defininf public variables

As your original code is
Code:
Public StrTier1, StrTier11  As String
StrTier1 = "Category"
StrTier11 = "category"
are you trying to create constants? If so you would use CONST.

Public Const StrTier1 as String = "Category"
Public Const StrTier11 as String = "category"

In this case I'd use C_Tier1 and C_Tier11 so you know your dealing with a Constant that you added.
Mark_ is offline   Reply With Quote
Old 12-08-2017, 01:28 PM   #10
The_Doc_Man
AWF VIP
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 10,876
Thanks: 38
Thanked 886 Times in 798 Posts
The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all The_Doc_Man is a name known to all
Re: Declaring adn defininf public variables

Good point, Mark. The only warning is if you ever pass one of those constants to a sub by reference, it gets confusing. I don't recall if you can actually do that, but if you do, it doesn't have the effect originally intended for the subroutine.
__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over six months and survived being home all day with the wife. She must really love me.
The_Doc_Man is offline   Reply With Quote
Old 12-09-2017, 05:48 AM   #11
jleach
Newly Registered User
 
Join Date: Jan 2012
Location: New York, New York
Posts: 117
Thanks: 6
Thanked 23 Times in 23 Posts
jleach will become famous soon enough
Re: Declaring adn defininf public variables

Quote:
Originally Posted by The_Doc_Man View Post
"Public" in a class module is close to totally meaningless.
Curious why you say that. Public and private methods and properties have as much use in class modules as they do anywhere else:

Code:
'Book class
Option Explicit

Private mPageCount As Integer
Private mCurrentPage As Integer

'readonly with internal validation
Public Property Get CurrentPage() As Integer
  CurrentPage = mCurrentPage
End Property
Private Property Set CurrentPage(i As Integer)
  If i < 1 OR > Me.PageCount Then Err.Raise 9 'out of range
  mCurrentPage = i
End Property

'readonly
Public Property Get PageCount() As Integer
  PageCount = mPageCount
End Property
Private Property Set PageCount(i as Integer)
  mPageCount = CalculatePageCount()
End Property

Public Sub GotoPage(PageNumber As Integer) 
  Me.CurrentPage = PageNumber
  ... other stuff
End Sub
A little rough around the edges but serves well enough for an example. I'm not sure I've ever written a module where I didn't want most of the implementation hidden in private procedures and have only the interface exposed through public ones.

Cheers,

__________________
- Jack D. Leach - Dymeng Services
jleach is online now   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring Variables steve_4477 Modules & VBA 2 06-30-2011 10:14 AM
declaring variables Mechele Reports 2 11-27-2006 02:54 PM
Declaring and using Public Functions JoshuaAnthony Modules & VBA 2 01-29-2004 01:45 PM
question about declaring variables BigJimSlade Modules & VBA 4 01-16-2004 03:15 PM
Declaring Variables aziz rasul Modules & VBA 1 03-27-2003 11:39 AM




All times are GMT -8. The time now is 04:31 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Sponsored Links

How to advertise

Media Kit


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World