Carry a varible to a new routine

G

Guest

I have a form were I am capturing some information. When I get to the end of the routine I have it call on another routine. but the value of the variable does not carry through from the previous routine. The original routine captures information from a userform. I am posting the last part of the code

With Me.CheckBox
If Me.CheckBox2.Value = True The
strto2 = "you da man
Els
Me.CheckBox2 = Fals
strto2 = "you not the man
End I
MsgBox "" & strto 'for testing purposes onl
MsgBox "" & strto
End Wit
Me.Hid
Call testdatebo
End Su

I want carry the strto and strto2 to the testdatebox routine. Any help would be great thanks
 
B

Bob Phillips

Declare them as public variables in a standard code module.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

GReg said:
I have a form were I am capturing some information. When I get to the end
of the routine I have it call on another routine. but the value of the
variable does not carry through from the previous routine. The original
routine captures information from a userform. I am posting the last part of
the code.
With Me.CheckBox2
If Me.CheckBox2.Value = True Then
strto2 = "you da man"
Else
Me.CheckBox2 = False
strto2 = "you not the man"
End If
MsgBox "" & strto 'for testing purposes only
MsgBox "" & strto2
End With
Me.Hide
Call testdatebox
End Sub

I want carry the strto and strto2 to the testdatebox routine. Any help
would be great thanks
 
P

Patrick Molloy

Method 1.
Declarevariable in the general part of the module...ie at the top:

Option Explicit
Private msg as string

Public Sub TestMessage()

msg = "Hello World!"

ShowMessage

End Sub

Private Sub ShowMessage()
msgbox msg
End Sub

Method 2 ( preffered)
...pass required values...make the code more robust & re-usable

Option Explicit
Public Sub TestMessage1()
msg = "Hello World!"
ShowMessage msg
End Sub
Public Sub TestMessage2()
msg = "Hello World Again!"
ShowMessage msg
End Sub
Private Sub ShowMessage(text as string)
msgbox text
End Sub


--
Patrick Molloy
Microsoft Excel MVP
---------------------------------
GReg said:
I have a form were I am capturing some information. When I get to the end
of the routine I have it call on another routine. but the value of the
variable does not carry through from the previous routine. The original
routine captures information from a userform. I am posting the last part of
the code.
With Me.CheckBox2
If Me.CheckBox2.Value = True Then
strto2 = "you da man"
Else
Me.CheckBox2 = False
strto2 = "you not the man"
End If
MsgBox "" & strto 'for testing purposes only
MsgBox "" & strto2
End With
Me.Hide
Call testdatebox
End Sub

I want carry the strto and strto2 to the testdatebox routine. Any help
would be great thanks
 
G

Guest

Bob, I guess I am not sure were I would need to make them public. I am posting the code for the userform
Sub UserForm_Click(
End Su

Sub bOk_Click(
With Me.CheckBox
If Me.CheckBox1.Value = True The
strto = "chk1on
Els
Me.CheckBox1 = Fals
strto = "chk1off
End I
End Wit
'checkbox2 procedur
With Me.CheckBox
If Me.CheckBox2.Value = True The
strto2 = "chk2on
Els
Me.CheckBox2 = Fals
strto2 = "chk2off
End I
MsgBox "" & strto 'for testing purposes onl
MsgBox "" & strto
End Wit
Me.Hid
Call testdatebo
End Su

----- Bob Phillips wrote: ----

Declare them as public variables in a standard code module

--

HT

Bob Phillip
... looking out across Poole Harbour to the Purbeck
(remove nothere from the email address if mailing direct

GReg said:
I have a form were I am capturing some information. When I get to the en
of the routine I have it call on another routine. but the value of th
variable does not carry through from the previous routine. The origina
routine captures information from a userform. I am posting the last part o
the code
If Me.CheckBox2.Value = True The
strto2 = "you da man
Els
Me.CheckBox2 = Fals
strto2 = "you not the man
End I
MsgBox "" & strto 'for testing purposes onl
MsgBox "" & strto
End Wit
Me.Hid
Call testdatebo
End Su
would be great thank
 
B

Bob Phillips

Greg,

If the other routine is in the same userform module, you just declare it at
the top of that module

Public strto As String
Public strto2 As String

Sub UserForm_Click()
End Sub

Sub bOk_Click()
With Me.CheckBox1

etc.

but if it is to be used in another module, declare it in a standard code
module, at the start again

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Greg said:
Bob, I guess I am not sure were I would need to make them public. I am
posting the code for the userform.
 
G

Guest

Bob, That did it. I had to create it as its own module
Thank

----- Bob Phillips wrote: ----

Greg

If the other routine is in the same userform module, you just declare it a
the top of that modul

Public strto As Strin
Public strto2 As Strin

Sub UserForm_Click(
End Su

Sub bOk_Click(
With Me.CheckBox

etc

but if it is to be used in another module, declare it in a standard cod
module, at the start agai

--

HT

Bob Phillip
... looking out across Poole Harbour to the Purbeck
(remove nothere from the email address if mailing direct

Greg said:
Bob, I guess I am not sure were I would need to make them public. I a
posting the code for the userform
 
T

Tom Ogilvy

Just to add (since you seem to be struggling).

In the vbe, do Insert=>Module

this gives you the standard/general module Bob speaks of. At the top of the
module put in
Public strto As String
Public strto2 As String

Do this outside any procedure declarations, at the very top.

Of course you can do this in an existing standard/general module as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top