![]() |
Scope of variable includes all Form _and_ Code modules??
Is it possible to declare a public variable so that it is within scope for
all Form and Code modules? Thanks. John Wirt |
Scope of variable includes all Form _and_ Code modules??
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." "John Wirt" wrote in message ... Is it possible to declare a public variable so that it is within scope for all Form and Code modules? Thanks. John Wirt |
Scope of variable includes all Form _and_ Code modules??
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. -- HTH Bob Phillips "kiat" wrote in message ... 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." "John Wirt" wrote in message ... Is it possible to declare a public variable so that it is within scope for all Form and Code modules? Thanks. John Wirt |
Scope of variable includes all Form _and_ Code modules??
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. |
Scope of variable includes all Form _and_ Code modules??
Do you realize that when you declare the same variable name twice, it's
two distinct variables? |
Scope of variable includes all Form _and_ Code modules??
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! -- HTH Bob Phillips "John Wirt" wrote in message ... 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) "kiat" wrote in message ... 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." "John Wirt" wrote in message ... Is it possible to declare a public variable so that it is within scope for all Form and Code modules? Thanks. John Wirt |
All times are GMT +1. The time now is 01:25 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com