Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default can a userform pass an argument?

can a userform pass an argument back to a module? if so, could you please
elaborate?

Thanks.

Mike
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default can a userform pass an argument?

Hi
one way:
use a public variable

--
Regards
Frank Kabel
Frankfurt, Germany

"smokiibear" schrieb im Newsbeitrag
...
can a userform pass an argument back to a module? if so, could you

please
elaborate?

Thanks.

Mike


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default can a userform pass an argument?

Frank....I have globally declare my variables, but they are not passing
back with the values they are assigned in the form. Any ideas?

Mike

"Frank Kabel" wrote in news:uuUZMB82EHA.2572
@tk2msftngp13.phx.gbl:

Hi
one way:
use a public variable

--
Regards
Frank Kabel
Frankfurt, Germany

"smokiibear" schrieb im Newsbeitrag
...
can a userform pass an argument back to a module? if so, could you

please
elaborate?

Thanks.

Mike



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default can a userform pass an argument?

Frank....I have globally declare my variables, but they are not passing
back with the values they are assigned in the form. Any ideas?

Mike

"Frank Kabel" wrote in news:uuUZMB82EHA.2572
@tk2msftngp13.phx.gbl:

Hi
one way:
use a public variable

--
Regards
Frank Kabel
Frankfurt, Germany

"smokiibear" schrieb im Newsbeitrag
...
can a userform pass an argument back to a module? if so, could you

please
elaborate?

Thanks.

Mike




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default can a userform pass an argument?

Hi
if you have them globally declared it SHOULD work. Are you sure you
don't have declared them twice (globally AND in the form)

--
Regards
Frank Kabel
Frankfurt, Germany

"smokiibear" schrieb im Newsbeitrag
...
Frank....I have globally declare my variables, but they are not

passing
back with the values they are assigned in the form. Any ideas?

Mike

"Frank Kabel" wrote in news:uuUZMB82EHA.2572
@tk2msftngp13.phx.gbl:

Hi
one way:
use a public variable

--
Regards
Frank Kabel
Frankfurt, Germany

"smokiibear" schrieb im Newsbeitrag
...
can a userform pass an argument back to a module? if so, could

you
please
elaborate?

Thanks.

Mike







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default can a userform pass an argument?

after an option explicit statement and prior to my sub () statement, i
decalare several variables, of which are being referenced in the form.
the form obtains the proper values, but the variables are cleared when I
hide the form.


"Frank Kabel" wrote in news:eaCW1a82EHA.1396
@tk2msftngp13.phx.gbl:

Hi
if you have them globally declared it SHOULD work. Are you sure you
don't have declared them twice (globally AND in the form)

--
Regards
Frank Kabel
Frankfurt, Germany

"smokiibear" schrieb im Newsbeitrag
...
Frank....I have globally declare my variables, but they are not

passing
back with the values they are assigned in the form. Any ideas?

Mike

"Frank Kabel" wrote in news:uuUZMB82EHA.2572
@tk2msftngp13.phx.gbl:

Hi
one way:
use a public variable

--
Regards
Frank Kabel
Frankfurt, Germany

"smokiibear" schrieb im Newsbeitrag
...
can a userform pass an argument back to a module? if so, could

you
please
elaborate?

Thanks.

Mike






  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default can a userform pass an argument?

Mike,

smokiibear wrote:
if you have them globally declared it SHOULD work. Are you sure you
don't have declared them twice (globally AND in the form)

after an option explicit statement and prior to my sub () statement, i
decalare several variables, of which are being referenced in the form.
the form obtains the proper values, but the variables are cleared
when I hide the form.


You are probably declaring the variables in the UserForm. Those variables
will only retain their values for the lifetime of the UserForm. If you want
the variables to be true Global variables, you must declare them as Public
in a standard module.

If you want to leave them in your UserForm, you have to grab the values
before unloading the UserForm. And you have to preface the variable name
with the codename of the UserForm:

Sub Test()
UserForm1.Show
MsgBox UserForm1.MyVariable
Unload UserForm1
End Sub

'/ code behind UserForm

Public MyVariable As String

Private Sub cmdOK_Click()
MyVariable="OK"
Hide
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default can a userform pass an argument?

"Jake Marx" wrote in
:

Mike,

smokiibear wrote:
if you have them globally declared it SHOULD work. Are you sure you
don't have declared them twice (globally AND in the form)

after an option explicit statement and prior to my sub () statement,
i decalare several variables, of which are being referenced in the
form. the form obtains the proper values, but the variables are
cleared when I hide the form.


You are probably declaring the variables in the UserForm. Those
variables will only retain their values for the lifetime of the
UserForm. If you want the variables to be true Global variables, you
must declare them as Public in a standard module.

If you want to leave them in your UserForm, you have to grab the
values before unloading the UserForm. And you have to preface the
variable name with the codename of the UserForm:

Sub Test()
UserForm1.Show
MsgBox UserForm1.MyVariable
Unload UserForm1
End Sub

'/ code behind UserForm

Public MyVariable As String

Private Sub cmdOK_Click()
MyVariable="OK"
Hide
End Sub


I resolved the problem, but not sure why one way worked and the other
didn't. I had something like this (using your example):

Private Sub cmdOK_Click()
MyVariable = userform1.myvariable.value
Hide
End Sub

however, when I followed your example for the private routine, then
declared

MyVariable = userform1.myvariable.value

in the test routine, it worked fine.



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default can a userform pass an argument?

Hi Mike

I really dislike the use of public variables for those purposes, so allow me
to mention an alternative solution. Variables to be passed to a Sub or
Function are placed withinits parentheses:

Sub Test(S As String)
MsgBox "Someone passed " & S
End Sub

Now in your userform:

Private Sub CommandButton1_Click()
Call Test(Me.TextBox1.Text)
End Sub

HTH. Best wishes Harald


"smokiibear" skrev i melding
...
can a userform pass an argument back to a module? if so, could you please
elaborate?

Thanks.

Mike



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 188
Default can a userform pass an argument?

"Harald Staff" wrote in message
...

Hi Mike

I really dislike the use of public variables for those purposes, so
allow me to mention an alternative solution. Variables to be passed
to a Sub or Function are placed withinits parentheses:

Sub Test(S As String)
MsgBox "Someone passed " & S
End Sub

Now in your userform:

Private Sub CommandButton1_Click()
Call Test(Me.TextBox1.Text)
End Sub

HTH. Best wishes Harald


Hi Harald,

How about if the sub calls the userform (rather than the other way
around as you have shown above)?

What is the best practice way to pass a variable back to the calling
sub?

Thanks,

Alan.




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 575
Default can a userform pass an argument?

Another alternative (I don't like globals either) is to set an invisible
label's caption on the form to return the value you want.

Sub Launch
frm1.Show
strReturn = frm1.lblValue.Caption
Unload frm1
End Sub

in form code
cmdOK_Click
lblValue.Caption = "Something"
Me.Hide
End Sub

Robin Hammond
www.enhanceddatasystems.com

"smokiibear" wrote in message
...
can a userform pass an argument back to a module? if so, could you please
elaborate?

Thanks.

Mike



  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default can a userform pass an argument?

The "official" way to pass information between a form and a procedure is to create
properties for the form using property procedures. How about a simple example?

I have a simple userform, UserForm1, with two option buttons (captions Yes and No)
and a commandbutton (Close). The close button only hides the form, doesn't unload
it, so it and its buttons are still in memory. Here is the code behind the form:

'================================================= =====
' UserForm1 Code Module

Option Explicit

Public Property Get YesOrNo() As String
Select Case True
Case Me.OptionButton1.Value
YesOrNo = "Yes"
Case Me.OptionButton2.Value
YesOrNo = "No"
End Select
End Property

Public Property Let YesOrNo(ByVal sNewValue As String)
If UCase$(sNewValue) = "NO" Then
Me.OptionButton2.Value = True
Else
Me.OptionButton1.Value = True
End If
End Property

Private Sub CommandButton1_Click()
Me.Hide
End Sub
'================================================= =====

Property Let means the procedure can assign a value to the property by saying, e.g.,
UserForm1.YesOrNo = "Yes". When you assign the property in this way, the code within
the Let procedure is run, which in this case, selectes the appropriate option button.

Property Get is how the procedure learns the value of the property, using
UserForm1.YesOrNo to run the Get procedure, which in this case returns Yes or No
based on which option button is selected.

Here's the code within a simple procedure in a regular code module. It first asks a
Yes or No questions, instantiates the form, sends the preliminary response to the
form and shows the form. When the form is hidden, it asks the form what the final
answer is, and displays that.

'================================================= =====
' Regular procedure in a regular code module

Sub Test()
Dim lResponse As Long ' pass to form
Dim sResponse As String ' get from form
Dim frm1 As UserForm1

'ask user for initial setting
lResponse = MsgBox("Yes or No", vbQuestion + vbYesNo)

' turn setting into a string
sResponse = IIf(lResponse = vbYes, "Yes", "No")

' start up the form
Set frm1 = New UserForm1
With frm1
' pass variable to form
.YesOrNo = sResponse

.Show

' get new value back from the form
sResponse = .YesOrNo

End With
Unload frm1

' tell user what new value is
MsgBox "Form returned " & sResponse, vbExclamation

End Sub
'================================================= =====


- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______


smokiibear wrote:

can a userform pass an argument back to a module? if so, could you please
elaborate?

Thanks.

Mike


  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default can a userform pass an argument?

I've punched up my description below, and now it resides on my web site:

http://peltiertech.com/Excel/PropertyProcedures.html

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

Jon Peltier wrote:

The "official" way to pass information between a form and a procedure is
to create properties for the form using property procedures. How about a
simple example?

I have a simple userform, UserForm1, with two option buttons (captions
Yes and No) and a commandbutton (Close). The close button only hides the
form, doesn't unload it, so it and its buttons are still in memory. Here
is the code behind the form:

'================================================= =====
' UserForm1 Code Module

Option Explicit

Public Property Get YesOrNo() As String
Select Case True
Case Me.OptionButton1.Value
YesOrNo = "Yes"
Case Me.OptionButton2.Value
YesOrNo = "No"
End Select
End Property

Public Property Let YesOrNo(ByVal sNewValue As String)
If UCase$(sNewValue) = "NO" Then
Me.OptionButton2.Value = True
Else
Me.OptionButton1.Value = True
End If
End Property

Private Sub CommandButton1_Click()
Me.Hide
End Sub
'================================================= =====

Property Let means the procedure can assign a value to the property by
saying, e.g., UserForm1.YesOrNo = "Yes". When you assign the property in
this way, the code within the Let procedure is run, which in this case,
selectes the appropriate option button.

Property Get is how the procedure learns the value of the property,
using UserForm1.YesOrNo to run the Get procedure, which in this case
returns Yes or No based on which option button is selected.

Here's the code within a simple procedure in a regular code module. It
first asks a Yes or No questions, instantiates the form, sends the
preliminary response to the form and shows the form. When the form is
hidden, it asks the form what the final answer is, and displays that.

'================================================= =====
' Regular procedure in a regular code module

Sub Test()
Dim lResponse As Long ' pass to form
Dim sResponse As String ' get from form
Dim frm1 As UserForm1

'ask user for initial setting
lResponse = MsgBox("Yes or No", vbQuestion + vbYesNo)

' turn setting into a string
sResponse = IIf(lResponse = vbYes, "Yes", "No")

' start up the form
Set frm1 = New UserForm1
With frm1
' pass variable to form
.YesOrNo = sResponse

.Show

' get new value back from the form
sResponse = .YesOrNo

End With
Unload frm1

' tell user what new value is
MsgBox "Form returned " & sResponse, vbExclamation

End Sub
'================================================= =====


- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______


smokiibear wrote:

can a userform pass an argument back to a module? if so, could you
please elaborate?

Thanks.

Mike




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
pass argument to macro tommy Excel Programming 4 September 1st 04 06:21 PM
Pass an argument to Excel workbook dorothy lo Excel Programming 2 April 22nd 04 04:05 AM
How to pass an Excel range as an argument to a SQL Server stored Procedure Belinda Excel Programming 7 April 8th 04 11:24 AM
How to pass values from a userform to a standard module? TBA[_2_] Excel Programming 3 January 7th 04 01:50 PM
How to pass arguments from ThisWorkbook to a UserForm strataguru[_4_] Excel Programming 1 October 7th 03 11:29 PM


All times are GMT +1. The time now is 06:54 PM.

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"