Scope of variable includes all Form _and_ Code modules??

  • Thread starter Thread starter John Wirt
  • Start date Start date
J

John Wirt

Is it possible to declare a public variable so that it is within scope for
all Form and Code modules?

Thanks.

John Wirt
 
Yes, it's possible. Simply declare them variables in a module using Public
keyword, Public p_i As Long, Public p_Const As String = "This is a string."
 
A publicly declared variable in a class module is also available to the
other modules as long as you precede it by the class name.

For instance, declare a public variable cWorksheets in ThisWorkbook module,
and form anywhere else you can reference it with this code

Thisworkbook.cWorksheets=17

same with Forms, Worksheets.
 
I do not think this works.

I declared the variable numPrintOrder in a module 15 (one where the variable
is used in a procedure) thusly,

Option Explicit
Public numPrintOrder as integer

----------------------------------------
The value of numPrintOrder is set from the values entered in .textboxes on
frmUserForm8.

Private Sub butPrintOK_Click()
Dim numCopiestoPrint As Integer
Dim numPagestoPrint As Integer
Dim numPrintOrder As Integer

With frmUserForm10
If txtNumCopies.Value = "0" Then
numCopiestoPrint = 1
ElseIf txtNumCopies.Value <> "" Then
numCopiestoPrint = txtNumCopies.Value
Else
numCopiestoPrint = 1
End If
If txtNumPages.Value = "0" Then
numPagestoPrint = 0
ElseIf txtNumPages.Value <> "" Then
numPagestoPrint = txtNumPages.Value
Else
numPagestoPrint = 0
End If
End With

numPrintOrder = 100 * numPagestoPrint + numCopiestoPrint

------------------

I tried calling frmUserForm10 from code module 15, like this:

Option Explicit
Public numPrintOrder as Integer

Public Sub PrintCurrentPage() <-----Here???
Dim strWshName As String
Dim numCopiestoPrint As Integer
Dim numPagestoPrint As Integer

frmUserForm10.Show

strWshName = ActiveSheet.name

numCopiestoPrint = numPrintOrder Mod 100
numPagestoPrint = (numPrintOrder - numCopiestoPrint) / 100
If numPagestoPrint = 0 Then numPagestoPrint = 1

ActiveSheet.PrintOut _
From:=1, To:=numPagestoPrint, _
Copies:=numCopiestoPrint

End Sub
--------------------------------------------

This did not work. After fmUserForm 10 closed, the value of numPrintOrder is
0.

To get the value of numPrintOrder into the PrintCurrentPage procedure, I
split it in two after the frmUserForm10.show command and added a statement,

call PrintCurrentPage2(numPrintOrder)

This works. the printer control information entered in form 10 is entered in
the parameters of the PrintOut method.

---------------------------------
What is the problem here? How does one define a global variable in a code
module, set the value of the variable in a procedure within a form, and then
have the value of the variable be within scope inside a different procedure
in a code module?

As indicated above, I was only able to transfer the value of the variable as
set in the form procedure to the procedure in the code module, by calling it
with the variable as an input parameter. This hardly qualifies as being a
'global' variable.

Thanks in advance.

John Wirt















Call PrintCurrentPage2(numPrintOrder)
 
Do you realize that when you declare the same variable name twice, it's
two distinct variables?
 
John,

You have declared the variable numPrintOrder as a global variable in a code
module. Okay so far.

Unfortunately, you then declare it again as a local variable in the form
procedure butPrintOK_Click. Therefore when you reference numPrintOrder
within this form procedure, you use the local variable, but which is being
loaded, but when you reference numPrintOrder in other procs, you reference
the global variable, which is empty.

Get rid of the local variab le!
 
Back
Top