Link to home
Start Free TrialLog in
Avatar of const71
const71

asked on

Returning values from Modal Forms to its invoker

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?
Avatar of inthedark
inthedark
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of inthedark
inthedark
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
I think Dhaest means

Public MyButton_Click
  Dim MyPrimaryKeyID as Long
   frmModalForm.show vbmodal
  MyPrimaryKeyID =clng(frmModalForm.tag)
End Sub
That's right "inthedark". I was indeed clng forgotten.
Thx
Avatar of const71
const71

ASKER

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?
Avatar of const71

ASKER

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.