Pass variable from user form to main macro

B

Bob

I have a rather long macro that, thanks to help I got here, works! I now
need to make some conditional changes. The code is easy to modify, but I
need to present a user form to get some information, including the type of
guide the user wants the macro to produce.

I've got a form (frmMain) with two option controls (typeStudent /
typeLeader) and a Submit command button (cmdSubmit). The form code is:

Dim typeStudent As Boolean
Dim typeLeader As Boolean
Private Sub cmdSelect_Click()
typeLeader = optGuideLeader.Value
typeStudent = optGuideStudent.Value
Me.Hide [also tried Unload Me]
End Sub

In the main macro, I have (for testing purposes):

frmMain.Show
If typeLeader Then Debug.Print "Leader Guide" Else Debug.Print "Student
Guide"

No matter which option I choose on the form, typeLeader in the macro is
always False. Setting a breakpoint before the Show command, I see that
typeLeader is set to True in the form code, but is then False in the macro
code.

I have tried all variations of using or leaving out Dim statements in both
the form and macro code with no difference in function.

According to Steve Roman's book "Writing Word Macros", I can probably move
code from the macro into the form, but there are several things I'd like to
ask on the form, and I'd like to run the form only once at the beginning of
the macro so the rest of the macro can run unattended.

Is there a way to pass a variable from frmMain into the main macro? If so,
any hints? If not, what would be the best alternative?

Thanks!
Bob
 
J

Jezebel

You should read up on variable scoping; and if you added 'Option Explicit'
to the top of all of your modules, the bug would have become obvious. The
problem is that 'typeLeader' declared in the form is not the same variable
as the 'typeLeader' referred to in your main macro. These are two
independent variables.

You could typeLeader as a global variable; but this is poor practice.

The good way to deal with this is to add a property to the form module; then
query that in the main macro --

--- IN the form ---
Public Property Get Leader() as boolean
Leader = typeLeader
End Property


--- In the Main Macro ---
Dim pForm as frmMain

set pForm = new frmMain
pForm.Show

if pForm.Leader then
Debug.Print "Leader Guide"
Else
Debug.Print "Student
end if

unload pForm
 
B

Bob

Thanks, Jezebel,

I thought I had "Option Explicit" set, but did not. I'll try using the
public property.

Thanks,
Bob

Jezebel said:
You should read up on variable scoping; and if you added 'Option Explicit'
to the top of all of your modules, the bug would have become obvious. The
problem is that 'typeLeader' declared in the form is not the same variable
as the 'typeLeader' referred to in your main macro. These are two
independent variables.

You could typeLeader as a global variable; but this is poor practice.

The good way to deal with this is to add a property to the form module; then
query that in the main macro --

--- IN the form ---
Public Property Get Leader() as boolean
Leader = typeLeader
End Property


--- In the Main Macro ---
Dim pForm as frmMain

set pForm = new frmMain
pForm.Show

if pForm.Leader then
Debug.Print "Leader Guide"
Else
Debug.Print "Student
end if

unload pForm







Bob said:
I have a rather long macro that, thanks to help I got here, works! I now
need to make some conditional changes. The code is easy to modify, but I
need to present a user form to get some information, including the type of
guide the user wants the macro to produce.

I've got a form (frmMain) with two option controls (typeStudent /
typeLeader) and a Submit command button (cmdSubmit). The form code is:

Dim typeStudent As Boolean
Dim typeLeader As Boolean
Private Sub cmdSelect_Click()
typeLeader = optGuideLeader.Value
typeStudent = optGuideStudent.Value
Me.Hide [also tried Unload Me]
End Sub

In the main macro, I have (for testing purposes):

frmMain.Show
If typeLeader Then Debug.Print "Leader Guide" Else Debug.Print "Student
Guide"

No matter which option I choose on the form, typeLeader in the macro is
always False. Setting a breakpoint before the Show command, I see that
typeLeader is set to True in the form code, but is then False in the macro
code.

I have tried all variations of using or leaving out Dim statements in both
the form and macro code with no difference in function.

According to Steve Roman's book "Writing Word Macros", I can probably move
code from the macro into the form, but there are several things I'd like
to
ask on the form, and I'd like to run the form only once at the beginning
of
the macro so the rest of the macro can run unattended.

Is there a way to pass a variable from frmMain into the main macro? If
so,
any hints? If not, what would be the best alternative?

Thanks!
Bob
 

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