Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return a value from a form? 1

Status
Not open for further replies.

venkman

Programmer
Oct 9, 2001
467
US
Is there a way to have a custom userform return a value?
like:
dim s as string
s = myform.show

or something?

-Venkman
 
yes, but your code doesn't make very much sense...
add a control to your form, ie a field (textbox)

add a button (commandbutton)

2x click the button, it should default goto to editor under event:

commandbutton1_click
activesheet.cells(1,1) =textbox1.value

end sub


now write a module:
sub tranpkp()
userform1.show
end sub


run tranpkp, the form will show, type something in the empty field. click the command button. .... BOOM.
[yinyang] Tranpkp [pc2]
************************************
- Let me know if this helped/worked!
Please remember to give helpful posts the stars they deserve!
This facilitates others navigating through the threads / posts!
 
I don't think you can actually use it to return a value. But you can get a value from any control on the userform (including hidden ones). So for example:
Code:
sub main()

  MyForm.show
  
  msgbox(myform.ReturnValue)

  MyForm.unload

end sub()
In the userform code:
Code:
sub OKButton_Click()
   ReturnValue= ..... 
   me.hide
end sub

and you'd define a control named "ReturnValue", with visible set to false, on the userform.
Does that help?
Rob
 
I'm sorry, I guess I wasn't speciffic enough. The question I'm asking is not how to set a value in a cell of a sheet to a value gotten from a form. I'm asking if I can get the value gotten from a form to a variable, without using a shared global variable.

For instance if I was able to override the default show sub so that it was now a function which returned a string. then the code I wrote before would work. Or is there a way to access a forms variables after it disappears?

As of now I'm using the shared global variable approach to solve my problem, but I don't like using global variables if I don't have to. I find they make the code harder to read later. Is there a way around using globals?

-Venkman
 
Venkman,
Did you see my reply to your post? I think it does what you need.
The form's controls are still accessible after the hide method, until the unload method is executed.
Rob
 
Rob,
You submitted your post as I was typing mine, so I was resonding the first reply. Let me try out your method, and I'll get back to you.
Thanks for the help.
-Venkman
 
Rob,
Your method worked great.
Thanks,
Venkman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top