Solved

Assigning  a value to a variable in another procedure

Posted on 2007-12-06
4
1,355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

623 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