Solved

Assigning  a value to a variable in another procedure

Posted on 2007-12-06
4
1,353 Views
Last Modified: 2012-06-22
I have been  using excel vba to develop some forms...I have been using text box values as values in my comutations...However since in some cases i need to format the text to a given decimal place i am loosing precision. So my solution is to assign the values to variables and format during the transision from variable to text box....for instance In the example below i am assigning a value to a text box in another form...along these lines how would i assign a value to a variable in a private sub linked to a different form..


'rad is a double variable 
 
 
Time_of_Concentration_Form.tb_hydro_rad_CHF5 = rad
'this works fine
 
 
Time_of_concentration_form.private sub main (local Variable) = rad
'this i can't do

Open in new window

0
Comment
Question by:ToddHawley4984
  • 2
  • 2
4 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 20423552
Your local variable has local scope and is inaccessible from outside the form.  The preferred method to keep state in your userform is to use a module level variable and access it with a public property

In your user form:

Private mRad As Double

Private Sub Main()
    mRad = 5  'Set however you set this variable.
End Sub

Public Property Get Rad() As Double
    Rad = mRad
End Property

Public Property Let Rad(Value As Double)
    mRad = Value
End Property


Now you can assign the value using the public property from other code:

Time_of_Concentration_Form.Rad = rad




0
 

Author Comment

by:ToddHawley4984
ID: 20423684
I've only been programming in VBA for about a week...I understand that its a module level variable but I don't quite understand how to manipulate it

0
 

Author Comment

by:ToddHawley4984
ID: 20423696
If i'm gonna use a given value in different form should i use a public variable delared in my module?  Is there any real reason not to use public variables other than memory consumption?  I realize if i have a public variable i have to be careful about changing in from different procedures
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 50 total points
ID: 20424490
Using a public variable is a possibility.  The private with property method I showed above would work the same as if you declared a public variable:

Public Rad As Double

Private Sub Main()
    Rad = 5  'Set however you set this variable.
End Sub

Memory consumption isn't much of an issue when we're talking about one double value.  It's only 8 bytes and you would still use that with a private module level variable.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question