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!

Call Modal form as Function 2

Status
Not open for further replies.

RBCDS

Technical User
Apr 6, 2001
24
0
0
CA
I have an application that prompts users to save a profile as... What I would like is to call a function "saveAs()" that returns an ID value just as msgbox() returns a value.

My Save As dialog is designed to look similar to a regular Save As dialog box except the items are populated from a SQL DB.

I can accomplish this task using Public variables but I would prefer to avoid those in lieu of what I am trying to accomplish.

Code:
Private Sub mnuSaveAs_Click()

   intSaveAs = saveAs()    

End Sub
_______________________

private function saveAs() as Integer

   ' frm.Show 1, Me
   <code???>

End Function

Regards,

Dan
 
When I want a modal form to return a value to a calling form:
Say we've got frmCaller and frmCalled. I declare a Public variable at frmCalled's form level and set it with my return value. When I'm done with frmCalled, I Hide it instead of Unload it. This will release control back to frmCaller. After operation has returned to frmCaller, I check frmCalled's Public variable (frmCalled.ReturnValue) then say Unload frmCalled.

You still use a Public variable but it's not global, you have to qualify it with the form name so it's not quite so hazardous.
 

Ok, here is a real simple demo you can create for yourself...

You will need to start a new project and add a form, so you will have Form1 and Form2. To each form add a command button (command1) and to form2 add a text box (text1).

Now add the following code to form1
[tt]
Option Explicit

Private Sub Command1_Click()
MsgBox Form2.WhoWantsAValueReturnedToThem(Me)
End Sub
[/tt]

Then to form2 add the following code
[tt]
Option Explicit

Public Function WhoWantsAValueReturnedToThem(CallingForm As Form) As Integer

Me.Show vbModal, CallingForm
WhoWantsAValueReturnedToThem = Int(Text1.Text)

End Function

Private Sub Form_Load()
Text1.Text = 3
End Sub

Private Sub Command1_Click()

Unload Me

End Sub
[/tt]

This helps wrap up your called form as an object (or at least you can pretty much treat it as one as long at it does not have outside dependancies) and just gives you a one liner to retrieve a value.

Good Luck

 
Another method, is to use property let/set and get methods, which I guess is a more encapsulated way.



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
vb5prgrmr: I like the way you went about this with the single line of code, it also allows it to be re-used as often as necessary.

mattKnight: I did a few keyword searches and came up with your response from a post from before and ended up using the "property" method. It works nice.

Thanks guys

Dan
 
Thank you!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top