TheSwamp

Code Red => VB(A) => Topic started by: David Hall on December 19, 2008, 11:01:16 AM

Title: Form to Module or Module to Form?
Post by: David Hall on December 19, 2008, 11:01:16 AM
Which is better, to call a module from a form or to call a form from your module?  I have always had to create a sub that did nothinng more than call a form, gather my data, and then send it to another module to process.  Yesterday I got to thinking, (scary huh) why did I need 2 subs, why not have 1 that calls the form, and then processes the data on it.

Long story short, how do I capture the cancel button on the form to quit out of the calling sub?  The button has Unload Me, but that just dumps the form contents, but doesn't quit out of the calling sub.  I thought about checking the values of the data boxes on the form, but what if the user fills out all the  boxes, and then hits cancel.  The boxes would have data, so it would try and continue.  I guess I could use the cancel button to clear a data box, then check it for nil and dump that way.  Any suggestions?
Title: Re: Form to Module or Module to Form?
Post by: Matt__W on December 19, 2008, 11:07:25 AM
What about adding END after the unload me?  Or simply just END?  Or EXIT SUB?
Title: Re: Form to Module or Module to Form?
Post by: David Hall on December 19, 2008, 11:12:06 AM
End after the unload me did it.  I knew it had to be simple.  Thanks Matt
Title: Re: Form to Module or Module to Form?
Post by: Matt__W on December 19, 2008, 11:13:04 AM
End after the unload me did it.  I knew it had to be simple.  Thanks Matt
You're welcome.    :kewl:
Title: Re: Form to Module or Module to Form?
Post by: David Hall on December 19, 2008, 01:55:41 PM
But we still didn't answer the original quesstion, is it better to call a form and pass to a module, or let the module call the form and recieve the results
Title: Re: Form to Module or Module to Form?
Post by: rogue on December 29, 2008, 12:39:13 PM
I usually have the module driving the form, one of the reasons being situations similar to what you encountered. (Basically, you are wanting to return a value, or status, from a form call)

I use a Global variable, stored in a code Module. Whether that Global stores the results of calculations done on a form, or just sets the value of , say, gIntCancel to 1, you can set the value of your Global variable from your form code before you unload it.

The code execution then resumes in the calling code Module, conditionally dependent on what the Global variable is now set to.

A Little psuedo code:
'----------------------------------
' In a code module

Global retCode as Integer
Sub Test () 

    retCode =0
    frmCalculate.Show

    if retCode=1 then
        frmContinueCalc.Show
    else
        exit sub
   end if

End Sub
Title: Re: Form to Module or Module to Form?
Post by: Keith™ on December 29, 2008, 02:25:26 PM
I find it best to call a form from a module. In VB, you call your main form from Sub Main() so that is where I would start.

Now that we have that settled ...

In your form, you should use QueryClose as well to prevent errors in data processing should the user close the form using the control box.
Code: [Select]
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
 If CloseMode = 0 Then Cancel = 1
 Hide Me
End Sub

The window will be hidden, but the form along with the data will be accessible from the module.

Of course you could always set the data from the form, thus you no longer need to query the form after it is hidden.

Remember to unload the form after collecting all of your data. While VBA is pretty forgiving in that matter, VB is not so much. You can easily end up with multiple applications running in the background is you don't unload them.
Title: Re: Form to Module or Module to Form?
Post by: David Hall on December 30, 2008, 10:22:09 AM
thanks