Public variable declaration

Wysy

Registered User.
Local time
Today, 06:53
Joined
Jul 5, 2015
Messages
333
Hi,
I am struggling a while with this and i can not figure out what do i do wrong.
I have a form with a button that on click sets a variable
Code:
Public sub x_click
intX=me.text1
end sub
I want that variable to be passed for another sub.
Code:
Public x as integer
I put that before the sub
When i try to use this variable in another sub, it is not working.
What do i do wrong?
 
Why intX and not just x ?
It's best to put all global type var's in a separate module, much less confusing to correctly evaluate the scope vs. where they are
 
sorry, sure it is misspelled
 
1. Go to vba setting and check require variable declaration
2. This ensures option explicit at the top of all modules
3. Declare your variables
Option compare database
Option explicit 'once you change settings

Public sub x_click
dim intX as integer 'declare variable
intX=me.text1
' call some procedure passing intX
end sub

Public Sub SomeProcedure (intX as integer)
'do somethig with passed value
x = x + 1
end sub

t's best to put all global type var's in a separate module
Do not use global variables unless you absolutely have to which is never. That is poor programming. Class variables are fine. Pass variables to procedures.
 
1. Go to vba setting and check require variable declaration
2. This ensures option explicit at the top of all modules
3. Declare your variables
Option compare database
Option explicit 'once you change settings

Public sub x_click
dim intX as integer 'declare variable
intX=me.text1
' call some procedure passing intX
end sub

Public Sub SomeProcedure (intX as integer)
'do somethig with passed value
x = x + 1
end sub


Do not use global variables unless you absolutely have to which is never. That is poor programming. Class variables are fine. Pass variables to procedures.
I tend to use them in Excel some times, they are more handy there. :)
 
It runs with an Argument not Optional

Code:
Option Explicit
Public intX As Integer
Public Sub x_click()
Dim intX As Integer 'declare variable
intX = 100
' call some procedure passing intX
Call SomeProcedure
End Sub

Public Sub SomeProcedure(intX As Integer)
'do somethig with passed value
MsgBox intX
End Sub
 
SomeProcedure is Expecting an Argument:

' call some procedure passing intX
Call SomeProcedure(intX)

or change the Sub to:

Public Sub SomeProcedure()
Msgbox intX
end Sub


' call some procedure passing intX
Call SomeProcedure
 
Thank you, i corrected my mistake. I have put the public variable declaration into a module and it works fine
thank you!
 
be warned that Public variables get easily destroyed when a runtime error occurs.
you can always use [Tempvars] (they are public variables also).
 
be warned that Public variables get easily destroyed when a runtime error occurs.
you can always use [Tempvars] (they are public variables also).
This is a very common myth. Errors do not cause variables to be destroyed. It happens when the project is reset following a break. Resetting is easily avoided.

TempVars are a complex solution looking for a simple problem to solve.
 

Users who are viewing this thread

Back
Top Bottom