Passing values between modules

L

liz

I am following jay Freedman's advice about structuring my modules. I
have a user form and a module that will update some of the document
properties and save the file. My question relates to the user
selecting the Cancel button. I am hiding the form in the form Cancel
sub procedure, control will them pass back to the calling module which
looks like this:

Set frmCaptureFileProperties = New CaptureFileProperties
frmCaptureFileProperties.Show
Call UpdateDocumentProperties
Set frmCaptureFileProperties = Nothing

so on return from frmCaptureFileProperties.Show i could have an OK or
NOK situation. I would usually set a flag to indicate this, but how do
I hand this information back to the main module? I presume I could do
something like this if I can pick up the value of booUserCancelled:

Set frmCaptureFileProperties = New CaptureFileProperties
frmCaptureFileProperties.Show
if booUserCancelled = True goto ClearMemory
Call UpdateDocumentProperties
ClearMemory:
Set frmCaptureFileProperties = Nothing

Does booUserCancelled need to be Public, and if so where should I
declare it?
 
J

Jay Freedman

I am following jay Freedman's advice about structuring my modules. I
have a user form and a module that will update some of the document
properties and save the file. My question relates to the user
selecting the Cancel button. I am hiding the form in the form Cancel
sub procedure, control will them pass back to the calling module which
looks like this:

Set frmCaptureFileProperties = New CaptureFileProperties
frmCaptureFileProperties.Show
Call UpdateDocumentProperties
Set frmCaptureFileProperties = Nothing

so on return from frmCaptureFileProperties.Show i could have an OK or
NOK situation. I would usually set a flag to indicate this, but how do
I hand this information back to the main module? I presume I could do
something like this if I can pick up the value of booUserCancelled:

Set frmCaptureFileProperties = New CaptureFileProperties
frmCaptureFileProperties.Show
if booUserCancelled = True goto ClearMemory
Call UpdateDocumentProperties
ClearMemory:
Set frmCaptureFileProperties = Nothing

Does booUserCancelled need to be Public, and if so where should I
declare it?

Yes, it should be Public so it's available to both the module code and
the userform code.

You could make the flag work whether you declare it at the top of the
module or at the top of the userform code. The distinction is mainly
academic unless you expect to have more than one copy of the userform
launched in the same session.

Strictly speaking, though, you should declare the flag at the top of
the userform code. That makes it a property of the userform (so if
there are several copies of the userform, each has its own flag). In
the macro, you would prefix the name of the flag with the name of the
userform copy and a dot. In the macro code you showed, you would
manipulate it like this:

Set frmCaptureFileProperties = New CaptureFileProperties
frmCaptureFileProperties.booUserCancelled = False
frmCaptureFileProperties.Show
If frmCaptureFileProperties.booUserCancelled = True Then _
GoTo ClearMemory
Call UpdateDocumentProperties
ClearMemory:
Set frmCaptureFileProperties = Nothing

In the userform's cmdCancel_Click procedure you would set

booUserCancelled = True
Me.Hide

If the user clicks OK, then the flag will still be False.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
L

liz

Yes, it should be Public so it's available to both the module code and
the userform code.

You could make the flag work whether you declare it at the top of the
module or at the top of the userform code. The distinction is mainly
academic unless you expect to have more than one copy of the userform
launched in the same session.

Strictly speaking, though, you should declare the flag at the top of
the userform code. That makes it a property of the userform (so if
there are several copies of the userform, each has its own flag). In
the macro, you would prefix the name of the flag with the name of the
userform copy and a dot. In the macro code you showed, you would
manipulate it like this:

Set frmCaptureFileProperties = New CaptureFileProperties
frmCaptureFileProperties.booUserCancelled = False
frmCaptureFileProperties.Show
If frmCaptureFileProperties.booUserCancelled = True Then _
GoTo ClearMemory
Call UpdateDocumentProperties
ClearMemory:
Set frmCaptureFileProperties = Nothing

In the userform's cmdCancel_Click procedure you would set

booUserCancelled = True
Me.Hide

If the user clicks OK, then the flag will still be False.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Jay,

thank you - it's easy when you know how! vbery pleased with progress
and the help available hrough the forum

luca
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top