Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Return a value from Userform.Show()?

I have been relying heavily on global variables to return
a value (or values) from any Userform in my program.
Is this the only way to return a value from Userform.Show()?

Is it possible to try something like:

x = Userform.Show() ' ???

This might work for returning one value, but what if I
had multiple values to return from a userform?

Just curious what other options are available, because I
feel like the global variable method is not always very
efficient.

Thanks!



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Return a value from Userform.Show()?

One way, probably the best, is building a class where the userform is a part
of its dataset. I don't know how comfortable you are with classes.

So an easier faster way is this: On top of the userform module, create some
public variables, like

Public S as string
Public L as long
'as many as you need

Code for its OK button:

Private Sub CommandButton1_Click()
Me.S = Me.Textbox1.text
Me.L = Me.Combobox1.Listindex
Me.Hide
End Sub

Code for its cancel button:

Private Sub CommandButton2_Click()
Me.S = "cancelled"
Me.Hide
End Sub


And finally, main code i a standard module:

Sub Test
Dim UF as Userform1
Dim X as String, Y as Long
Set UF = New Userform1
Uf.Show
X = UF.S
Y = UF.L
Unload UF
If X = "cancelled" then
'something sad?
Else
Msgbox "You wrote " & X
End if
End Sub

HTH. Best wishes Harald



"Robert Crandal" skrev i melding
...
I have been relying heavily on global variables to return
a value (or values) from any Userform in my program.
Is this the only way to return a value from Userform.Show()?

Is it possible to try something like:

x = Userform.Show() ' ???

This might work for returning one value, but what if I
had multiple values to return from a userform?

Just curious what other options are available, because I
feel like the global variable method is not always very
efficient.

Thanks!





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Return a value from Userform.Show()?

I have been relying heavily on global variables to return
a value (or values) from any Userform in my program.
Is this the only way to return a value from Userform.Show()?

Is it possible to try something like:

x = Userform.Show() ' ???

This might work for returning one value, but what if I
had multiple values to return from a userform?

Just curious what other options are available, because I
feel like the global variable method is not always very
efficient.

Thanks!


Global variables are pretty much common in most programming languages.
Managing them efficiently, though, requires simple common sense gained
from experience. The beauty of using 'globals' is that they can be
accessed from anywhere at anytime, by any procedure.

Implementing an 'InitGlobals' procedure that runs at startup is a good
place to start. This will 'load' all the 'startup values' your project
needs at runtime. These values can be stored (registry or text file[s])
for retrieval when your project starts, OR they can be set at startup
(or anytime during runtime).

If using the Registry for storage, "SaveSettings()" and "GetSettings"
are the main VB[A] functions to use when the default hive for the HKCU
key suffices for your needs. Beyond that there's several Registry API
functions you can use to work anywhere within the Registry the current
user has UAC permissions to access for writing. I stopped using the
Registry some years ago when I made all my projects completely reg-free
so they can be run from a memstick on any machine without leaving
anything behind.

There's a number of ways to store startup/runtime values in text files,
all which depend on how the data is structured. The most common methods
employ using INI and DAT files for storing startup values, program
options, and user preferences.

INI files is what Windows used before the Registry was introduced.
These are structured plain text files that require using the
PrivateProfile APIs to use them.

DAT files are also plain text files, but their contents are in binary
format. The VB[A] functions used for this are "Put()" and "Get()", and
the data is stored in UDTs.

IMO the only downside of using globals is we need to be careful of
'overuse'! Otherwise, using globals are a very neat and efficient way
to manage 'shared' runtime data.<g

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


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
Return to UserForm Patrick C. Simonds Excel Programming 2 March 3rd 09 01:30 AM
Userform Show Noemi Excel Programming 3 September 7th 06 12:17 AM
show userform john tempest[_2_] Excel Programming 5 May 12th 06 06:07 PM
How to Show a userform from XLA sub Thibault Excel Programming 1 December 8th 03 03:36 PM


All times are GMT +1. The time now is 02:12 AM.

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

About Us

"It's about Microsoft Excel"