PDA

View Full Version : Passing variables to a specified control



theta
01-10-2013, 04:41 AM
Hi all...

I am experimenting with passing variables and values. The current project I am working on requires the user to click a command button > this launches a userform where the user makes a selection. I need to result to be sent to a specific control.

When the command button is clicked it should call a function, which defines the receiving control e.g.

Command Button 1 click > Function GetUserInput(frmUser1.tbResult)

So the function GetUserInput would launch a userform (frmUserInput) and the result would then be used to populate frmUser1.tbResult.

Any ideas?

Aflatoon
01-10-2013, 04:44 AM
Have the result stored as a public property of the called form, which should be hidden and not unloaded when done, and the calling code can then simply read the value of the property and return it.

theta
01-10-2013, 04:53 AM
Ok I think I know what you mean...

CommandButton1 on UserformX clicked
GetUserInput(ControlID) called > this launches UserformY to make a selection
ControlID is then populated from the UserformY value

To launch the userform, should I be using initialise / load / show ?

I researched the differences but am still unsure of their specifics? If you could shed some light it would be very much appreciated.

Aflatoon
01-10-2013, 05:01 AM
Assuming you have set up a property called ReturnValue:
Dim uf as frmUserInput
set uf = new frmUserInput
uf.show
' ideally you should add code to check the form wasn't cancelled
GetUserInput = uf.ReturnValue
set uf = nothing

theta
01-10-2013, 05:17 AM
Assuming you have set up a property called ReturnValue:
Dim uf as frmUserInput
set uf = new frmUserInput
uf.show
' ideally you should add code to check the form wasn't cancelled
GetUserInput = uf.ReturnValue
set uf = nothing

Please confirm what you mean by public property? I have not set one of these before.

I have used public variables, but not public properties... ?

Thanks

Aflatoon
01-10-2013, 05:22 AM
You can just use a Public variable but properties are better. Here's a simple example of a string property:
Private m_sReturnValue As String

Public Property Get ReturnValue() As String

ReturnValue = m_sReturnValue

End Property

Private Sub cmdOK_Click()
' set the value of the variable read by the property
m_sReturnValue = "some value"
Me.Hide
End Sub

theta
01-14-2013, 10:22 AM
Thanks Aflatoon...been working with this. Better than a public variable (read into it).

The only problem I am having is making the value return to the a specified object or control. How would I go about doing this?

I would like to be able to call a module e.g.

GetUserInput(frmReturnValueHere.tbText1.Value)

The GetUserInput would take this object or control, then launch frmUserInput and when "OK" is clicked, the value of tbInput.Value should be sent top the tbText1.Value

?

Aflatoon
01-14-2013, 10:42 AM
Just have the function return the value to the control passed to it. Not really sure why you're using a function rather than a sub for this. Why not have the function return the value to the calling code which can then assign it to the control?

theta
01-14-2013, 10:52 AM
That's the part im struggling with. Returning it to the control ?

Aflatoon
01-16-2013, 02:02 AM
Function GetUserInput() as string
dim uf as frmUserInput
set uf = new frmUserInput
uf.show
GetUserInput = uf.ReturnValue
set uf = nothing
end function

then the calling code doesn't pass the control, it simply uses:
somecontrol.value = GetUserInput