Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 125
Default Retaining value of a string from a textbox on a form ??

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Retaining value of a string from a textbox on a form ??

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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 125
Default Retaining value of a string from a textbox on a form ??

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.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Retaining value of a string from a textbox on a form ??

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.






  #5   Report Post  
Posted to microsoft.public.excel.programming
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.








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Retaining value of a string from a textbox on a form ??

in the Userform I had this:

Private Sub CommandButton1_Click()
MyModule
End Sub

Private Sub CommandButton2_Click()
Final
End Sub

Private Sub CommandButton3_Click()
Unload Me
Application.Visible = True
End Sub

Sub HideMyForm_Click()
MyTxtBoxValue = myform.TextBox1.Value
myform.Hide
Application.Visible = True
Application.Visible = False
myform.Show
End Sub


Since you showed no code that restored the Excel application, I put two
buttons on the form to call the MyModule and Final subs respectively. After
I clicked on the hidemyForm button, I clicked on them and each show the
value in the textbox (in the message box), so I had not problem seeing what
the value was. Other than that, I used the code you showed, although i
correct the spelling of Workbook_Open.

Also, not sure why you hide the form, make the application visible, then
hide it and then show the form again.

I also put in a button that allowed me to close the form and make the
application.Visible again.
--
Regards,
Tom Ogilvy

"Dan Thompson" wrote in message
...
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.








  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Retaining value of a string from a textbox on a form ??

How about assigning the textbox.value to a worksheet cell before hiding, then
referencing the cell?

Just curious, does this work?:
msgbox (MyForm.Mytextbox.Value)

--
Toby Erkson
Oregon, USA
Excel 2002 in Windows XP

"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.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set Textbox on a Form to Deafault John Calder New Users to Excel 7 July 28th 09 01:39 AM
text boxes retaining their last value on form close and restart NDBC Excel Discussion (Misc queries) 3 July 1st 09 12:55 PM
textbox on form Glenn Robertson Excel Programming 1 March 4th 04 11:29 AM
Separate String into string + value form Johnny[_6_] Excel Programming 3 February 27th 04 02:38 PM
Retaining text formatting in TextBox Tom Ogilvy Excel Programming 0 September 9th 03 08:05 PM


All times are GMT +1. The time now is 03:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"