ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Retreive a variable (https://www.excelbanter.com/excel-programming/409274-retreive-variable.html)

R P

Retreive a variable
 

Is it possible for my UserForm code to retrieve a variable declared and
set in the main procedure?

I am testing to see if the code is run on my computer or not. I already
have this Boolean value set in the main procedure but I do not see how to
retrieve this value from my UseForm code. I could run the test again from
the UserForm but I have run across this problem before and thought I might
seek a solution.

I should say that I have already thought of creating a Static function
that holds this value.
I am just curious if there is a easier (better) way?
--




cht13er

Retreive a variable
 
On Apr 12, 2:10 pm, "R P" wrote:
Is it possible for my UserForm code to retrieve a variable declared and
set in the main procedure?

I am testing to see if the code is run on my computer or not. I already
have this Boolean value set in the main procedure but I do not see how to
retrieve this value from my UseForm code. I could run the test again from
the UserForm but I have run across this problem before and thought I might
seek a solution.

I should say that I have already thought of creating a Static function
that holds this value.
I am just curious if there is a easier (better) way?
--


See if you can massage your code to look like this:

Private Sub MainProcedure()
...
...
bnVariable = ...
Call LoadForm(bnVariable)
End Sub

'--------------------------------
Private Sub LoadForm(Result as Boolean)
Load Form1
Form1.TextBox1.Value = Result
Form1.Show
End Sub



Is that what you're looking for??
Chris

R P

Retreive a variable
 
Actually I need the opposite. Something like this:

Sub Main()
Dim my_computer as Integer

If (InStr(1, LCase(Environ("UserProfile")), "randy") Or_
InStr(1, LCase(Environ("UserProfile")), "safety")) Then mycomputer = 1
..
..

End Sub

Private Sub UserForm_Initialize()
Dim my_computer as Integer

my_computer = Main.my_computer


End Sub

Except Main.my_computer does not work




"cht13er" wrote in message
...
On Apr 12, 2:10 pm, "R P" wrote:
Is it possible for my UserForm code to retrieve a variable declared
and
set in the main procedure?

I am testing to see if the code is run on my computer or not. I
already
have this Boolean value set in the main procedure but I do not see how to
retrieve this value from my UseForm code. I could run the test again from
the UserForm but I have run across this problem before and thought I
might
seek a solution.

I should say that I have already thought of creating a Static
function
that holds this value.
I am just curious if there is a easier (better) way?
--


See if you can massage your code to look like this:

Private Sub MainProcedure()
...
...
bnVariable = ...
Call LoadForm(bnVariable)
End Sub

'--------------------------------
Private Sub LoadForm(Result as Boolean)
Load Form1
Form1.TextBox1.Value = Result
Form1.Show
End Sub



Is that what you're looking for??
Chris




Rick Rothstein \(MVP - VB\)[_1714_]

Retreive a variable
 
InStr(1, LCase(Environ("UserProfile")), "safety")) Then mycomputer = 1

Assuming your use of mycomputer instead of my_computer was simply a typo,
add a Module to your project and put this declaration in its code window...

Public my_computer As Integer

Then, make sure you remove this line...

Dim my_computer as Integer

from the Main subroutine. As long as you do not declare a my_computer
variable in any of your procedures, every procedure will have (read and
write) access to the globally available my_computer variable.

Rick



"R P" wrote in message
. ..
Actually I need the opposite. Something like this:

Sub Main()
Dim my_computer as Integer

If (InStr(1, LCase(Environ("UserProfile")), "randy") Or_
InStr(1, LCase(Environ("UserProfile")), "safety")) Then mycomputer = 1
.
.

End Sub

Private Sub UserForm_Initialize()
Dim my_computer as Integer

my_computer = Main.my_computer


End Sub

Except Main.my_computer does not work




"cht13er" wrote in message
...
On Apr 12, 2:10 pm, "R P" wrote:
Is it possible for my UserForm code to retrieve a variable declared
and
set in the main procedure?

I am testing to see if the code is run on my computer or not. I
already
have this Boolean value set in the main procedure but I do not see how
to
retrieve this value from my UseForm code. I could run the test again
from
the UserForm but I have run across this problem before and thought I
might
seek a solution.

I should say that I have already thought of creating a Static
function
that holds this value.
I am just curious if there is a easier (better) way?
--


See if you can massage your code to look like this:

Private Sub MainProcedure()
...
...
bnVariable = ...
Call LoadForm(bnVariable)
End Sub

'--------------------------------
Private Sub LoadForm(Result as Boolean)
Load Form1
Form1.TextBox1.Value = Result
Form1.Show
End Sub



Is that what you're looking for??
Chris





R P

Retreive a variable
 
Thanks Rick,

Yes, that was a typo
I added that global declaration to my main module and it seems to
work great.
Was there a reason why you wanted me to add a module rather than
declaring it in my main module?


"Rick Rothstein (MVP - VB)" wrote in
message ...
InStr(1, LCase(Environ("UserProfile")), "safety")) Then mycomputer = 1


Assuming your use of mycomputer instead of my_computer was simply a typo,
add a Module to your project and put this declaration in its code
window...

Public my_computer As Integer

Then, make sure you remove this line...

Dim my_computer as Integer

from the Main subroutine. As long as you do not declare a my_computer
variable in any of your procedures, every procedure will have (read and
write) access to the globally available my_computer variable.

Rick



"R P" wrote in message
. ..
Actually I need the opposite. Something like this:

Sub Main()
Dim my_computer as Integer

If (InStr(1, LCase(Environ("UserProfile")), "randy") Or_
InStr(1, LCase(Environ("UserProfile")), "safety")) Then mycomputer = 1
.
.

End Sub

Private Sub UserForm_Initialize()
Dim my_computer as Integer

my_computer = Main.my_computer


End Sub

Except Main.my_computer does not work




"cht13er" wrote in message
...
On Apr 12, 2:10 pm, "R P" wrote:
Is it possible for my UserForm code to retrieve a variable declared
and
set in the main procedure?

I am testing to see if the code is run on my computer or not. I
already
have this Boolean value set in the main procedure but I do not see how
to
retrieve this value from my UseForm code. I could run the test again
from
the UserForm but I have run across this problem before and thought I
might
seek a solution.

I should say that I have already thought of creating a Static
function
that holds this value.
I am just curious if there is a easier (better) way?
--

See if you can massage your code to look like this:

Private Sub MainProcedure()
...
...
bnVariable = ...
Call LoadForm(bnVariable)
End Sub

'--------------------------------
Private Sub LoadForm(Result as Boolean)
Load Form1
Form1.TextBox1.Value = Result
Form1.Show
End Sub



Is that what you're looking for??
Chris







Rick Rothstein \(MVP - VB\)[_1715_]

Retreive a variable
 
I wasn't thinking when I wrote that... your Main subroutine is already in a
Module, so you can use that. Just put the Public variable declaration in the
(General)(Declarations) section; that is, not within any procedures.

Rick


"R P" wrote in message
. ..
Thanks Rick,

Yes, that was a typo
I added that global declaration to my main module and it seems to
work great.
Was there a reason why you wanted me to add a module rather than
declaring it in my main module?


"Rick Rothstein (MVP - VB)" wrote in
message ...
InStr(1, LCase(Environ("UserProfile")), "safety")) Then mycomputer = 1


Assuming your use of mycomputer instead of my_computer was simply a typo,
add a Module to your project and put this declaration in its code
window...

Public my_computer As Integer

Then, make sure you remove this line...

Dim my_computer as Integer

from the Main subroutine. As long as you do not declare a my_computer
variable in any of your procedures, every procedure will have (read and
write) access to the globally available my_computer variable.

Rick



"R P" wrote in message
. ..
Actually I need the opposite. Something like this:

Sub Main()
Dim my_computer as Integer

If (InStr(1, LCase(Environ("UserProfile")), "randy") Or_
InStr(1, LCase(Environ("UserProfile")), "safety")) Then mycomputer = 1
.
.

End Sub

Private Sub UserForm_Initialize()
Dim my_computer as Integer

my_computer = Main.my_computer


End Sub

Except Main.my_computer does not work




"cht13er" wrote in message
...
On Apr 12, 2:10 pm, "R P" wrote:
Is it possible for my UserForm code to retrieve a variable
declared and
set in the main procedure?

I am testing to see if the code is run on my computer or not. I
already
have this Boolean value set in the main procedure but I do not see how
to
retrieve this value from my UseForm code. I could run the test again
from
the UserForm but I have run across this problem before and thought I
might
seek a solution.

I should say that I have already thought of creating a Static
function
that holds this value.
I am just curious if there is a easier (better) way?
--

See if you can massage your code to look like this:

Private Sub MainProcedure()
...
...
bnVariable = ...
Call LoadForm(bnVariable)
End Sub

'--------------------------------
Private Sub LoadForm(Result as Boolean)
Load Form1
Form1.TextBox1.Value = Result
Form1.Show
End Sub



Is that what you're looking for??
Chris








All times are GMT +1. The time now is 01:14 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com