Returning values from Modal Forms to its invoker

const71
const71 used Ask the Experts™
on
I have an MDI child form that spawns a modal form and would like this modal form to return a long value back to the MDI child that invoked it.

Somthing like this would have been nice  :)

Public MyButton_Click
    Dim MyPrimaryKeyID as Long
    MyPrimaryKeyID = frmModalForm.Show vbModal
End Sub

What's the best way to achieve this in Visual Basic 6.0?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can almost do it like that:

' as soon as you show the form the form becomes an object
On error resume next
frmModalForm.Show vbModal
' this code will stop until the form is hidden

' when the code starts you can still see the objects on the form
MyPrimaryKeyID = frmModalForm.txtMyTextBox

You also have access to public variables on the form.

But if the form unloads expext an error.

Also the form is still in memory which is not good so it is better to do this:

Dim frm as form

Set frm=new frmModalForm
frm.PublicVar = "Pass data like this"
frm.Show vbModal ' form load event now fires

YourVars = Frm.PublicVars
YourOtherVars =frm.text1

' now remove the form from from memory
Set frm = Nothing

Better like this, incase there is a time on the form keeping the form active:

' now remove the form from from memory
Unload Frm
Set frm = Nothing
Dirk HaestProject manager

Commented:

Public MyButton_Click
   Dim MyPrimaryKeyID as Long
    frmModalForm.show vbmodal
   MyPrimaryKeyID = me.tag
End Sub


On your mdi-child

Private Sub Form_Unload(Cancel As Integer)
   frmModalForm.tag = longvalue
End Sub
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

I think Dhaest means

Public MyButton_Click
  Dim MyPrimaryKeyID as Long
   frmModalForm.show vbmodal
  MyPrimaryKeyID =clng(frmModalForm.tag)
End Sub
Dirk HaestProject manager

Commented:
That's right "inthedark". I was indeed clng forgotten.
Thx

Author

Commented:
Ok, so let's see if I got this straight. This is how I understand the solution to be as offered by inthedark ...

Private Sub ShowModalDialog
    Dim frm as Form

    Set frm = New frmModalForm
    frm.PublicVar = 12345         'Passing values to frm
    frm.Show vbModal              'Load event fires in frm

    ... (inside frm -> assign vars and process events)
    ... (inside frm -> call frm.Hide to initiate cleanup)

    MyVar = frm.PublicVar   'MDIChild regains focus and
                            'retrieves 'return' values
                            'before destroying frm
    Unload frm          
    Set frm = Nothing       'clean up finished
End Sub

Is this right?

Author

Commented:
Also, does the above code still work when the user clicks the 'X' on the form window??
It was tested with X close. It should also handle abnormal termination.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial