Author Topic: Form to Module or Module to Form?  (Read 2988 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Form to Module or Module to Form?
« 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?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Form to Module or Module to Form?
« Reply #1 on: December 19, 2008, 11:07:25 AM »
What about adding END after the unload me?  Or simply just END?  Or EXIT SUB?
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Form to Module or Module to Form?
« Reply #2 on: December 19, 2008, 11:12:06 AM »
End after the unload me did it.  I knew it had to be simple.  Thanks Matt
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Form to Module or Module to Form?
« Reply #3 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:
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Form to Module or Module to Form?
« Reply #4 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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

rogue

  • Guest
Re: Form to Module or Module to Form?
« Reply #5 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

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Form to Module or Module to Form?
« Reply #6 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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Form to Module or Module to Form?
« Reply #7 on: December 30, 2008, 10:22:09 AM »
thanks
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)