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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you 'Return' something from a form?

Status
Not open for further replies.

Brawn

Programmer
Jul 5, 2001
73
0
0
CA
Can you 'Return' something from a form?

*without knowing where the form was called from?

[neutral] Brawn

- The difference between genius and stupidity is that genius has its limits -
 
What are you trying to return? Could you use a global variable and assign a value to it from the form? Any details? If you choose to battle wits with the witless be prepared to lose.
[machinegun][ducky]
 
I've been working with forms for a while and I don't have a 'style' developed yet. I keep thinking there must be something more than just accessing global variables.

You know how you can get the return value of msgbox.
Code:
if msgbox("Press Yes for msg.",vbYesNo) = vbYes then
     msbbox(msg)
end if

Is thare a way of making a user defined form act the same.

Eg. A form that u Select a # from 1-10. and the form return the results.
[neutral] Brawn

- The difference between genius and stupidity is that genius has its limits -
 
The only way I can think, is to create a function that handles the form and returns the value.

Code:
public function getNumber()as integer
   dim returnValue as integer
   frmGetNumber.Show
   returnvalue = frmGetNumber.gm_intSelecteNumber
   frmGetNumber.Hide
   getNumber = returnValue
End function

...or something of the like. Brawn

- The difference between genius and stupidity is that genius has its limits -
 
A form is basically just a visual class, so - with a few restrictions - you can do much the same as with a class. The following (slightly contrived) example illustrates the use of both a public function and of a property get to return values from a form.

First create a form (Form1) with a command button on it, and drop in the following code:
[tt]
Option Explicit

Private Sub Command1_Click()
MsgBox Form2.myMsg("Click Command1 or Command2"), , "From Form Function"
Unload Form2 ' Deliberately unload the form here to demonstrate that property survives this
MsgBox Form2.ExitCode, , "From Form Property"
End Sub
[/tt]
Now create a second form (Form2) with a label and two command buttons. Then drop in the following code:
[tt]
Option Explicit

Private mvarDemo As Long

Private Sub Command1_Click()
mvarDemo = vbOK
CloseMe
End Sub

Private Sub Command2_Click()
mvarDemo = vbCancel
CloseMe
End Sub

Public Function myMsg(strMessage As String) As Variant
Me.Label1 = strMessage
Me.Show vbModal
myMsg = mvarDemo
End Function

Private Sub CloseMe()
Me.Hide
End Sub

Public Property Get ExitCode() As Long
ExitCode = mvarDemo
End Property
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top