Solved

Assigning  a value to a variable in another procedure

Posted on 2007-12-06
4
1,351 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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 …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now