View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Dan Thompson Dan Thompson is offline
external usenet poster
 
Posts: 125
Default Retaining value of a string from a textbox on a form ??

Hi Tom, I see you alot on these boards and your Great! you have helped me a
number
of times before, you seem to be a staple of this news group :)

Anyhow You are right sorry about being loose with my description, I should
have been more detailed.

here is what I should have showed for sample code

Workbook Open Code.
-----------------------
Private Workbok_Open()
Application.visible = False
MyForm.Show
End Sub

Moulde1 Code.
--------------------
Option Explicit
Public MyTxtBoxValue As String
Sub MyModule()
MsgBox (MyTxtBoxValue)
End Sub
--------------------

UserForm Code.
--------------------
Sub HideMyForm On_Click()
MyTxtBoxValue = MyForm.TextBox1.value
MyForm.Hide
Application.Visible = True
Applicatoin.Visible = False
MyForm.Show
End Sub
----------------------

Module1 Code
------------------
Sub Final()
msgbox (MyTxtBoxValue )
End sub
------------------

"Tom Ogilvy" wrote:

In the code you show, it isn't declared as a global variable. Even if it
was, doing the assignment where you show it won't work if you unloaded the
form. If the form is just hidden (which is a different command), then it
will work. In fact, as long as the userform is hidden and not unloaded, you
can access the textbox directly.

This works fine for everyone else, so I suspect you are being loose in your
terminology or you are doing it correctly.

this code is flawed:
Sub test()
Dim MyTxtString as String
MyForm.Show
MyTxtString = MyForm.Mytextbox.value
MyForm.Hide
MsgBox (MyTxTString)
End Sub()


by using the Show method with no arguments, your form is shown as Modal.
therefore the next line of code (the assignment) will not be executed until
the userform is either hidden or unloaded - this would have to be done in
the the code module of the userform itself. If you unload there, then when
you reach
MyTxtSTring = MyForm.Mytextbox.value
the userform has been unload and by referencing it again, you reload it as a
new instance (thus it has no values but the default values). MyForm.Hide
at that point is meaningless. And Msgbox(MyTxtString) will of course show
nothing if the default value of the textbox is nothing.

Sub test()
Dim MyTxtString as String
MyForm.Show
MyTxtString = MyForm.Mytextbox.value
Unload MyForm
MsgBox (MyTxTString)
End Sub()


In the Userform something like

Private Sub CmdOK_click()
me.hide
End Sub

---------------------------------------
if you wanted to use the global varible.
---------------------------------------
' declared at the top of the general module, not a sheet module
Public MyTxtString as String

Sub test()
MyForm.Show
MsgBox (MyTxTString)
End Sub()

in the Userform Module

Private Sub CmdOK_Click()
MyTxtString = MyForm.Mytextbox.value
Unload Me
End Sub

--
Regards,
Tom Ogilvy


"Dan Thompson" wrote in message
...
Thanks for your input Bernie. However I already tried that and it doesn't

work
I think it's realy retarded that you can't assign the value of the textbox
to a public global variable and have VBA maintain that value in memeory

even
if the form with the txtbox is hidden it shouldn't matter because the

value
was passed to the Public Global variable before the form was hidden. Im
begining to think that this is not possible with VBA. The only work around

I
can think of is to have the value stored and saved to a text file and than
recal that string from the text file, But I realy don't want to have to do
that ??


"Bernie Deitrick" wrote:

Dan,

Try declaring it as a public variable in a standard codemodule, not the
userform codemodule.

HTH,
Bernie
MS Excel MVP
"Dan Thompson" wrote in message
...
I am working with a form which has a textbox that I am entering text

into
and
than passing the the string value entered into the text box on my form

to
a
string variable.
for example...

Sub test()
Dim MyTxtString as String
MyForm.Show
MyTxtString = MyForm.Mytextbox.value
MyForm.Hide
MsgBox (MyTxTString)
End Sub()

The problem is that the string from the textbox on MyForm does get
assigned
to MyTxtString variable, however as soon as the form his hidden the
variable
MyTxtString looses the value entered into the textbox and the value of
MyTxtString becomes "" (nothing).
How do I maintain the scope of MyTxtString to keep the value assigned

to
it
even when the form is hidden ?

I have already tried declaring MyTxtString as a Public Global variable

but
the scope and life of MyTxtString still is not maintained.
What am I doing wrong ?

Dan.