I have a variable that I set in a text change procedure. How can I use this same variable and its value in a different procedure?
Printable View
I have a variable that I set in a text change procedure. How can I use this same variable and its value in a different procedure?
Declare it modular, at the top, not inside the Sub.
Another possible way would be sending the variable as parameter, but that's only possible if you're calling the other Sub from inside that TextBox Change event.Code:Option Explicit
Private YourVarName as Something
...
Thanks jcis for the response. By using the option explicit do I have to initially declare all variables?
yes... or visual basic will give you the right error...Quote:
Originally Posted by papafrancisco
if you don't use it the variables used will be variant type...
Maybe I'm dreaming but didn't the older quick basic allowed a variable to be used or tested anywhere?
Don't know bout that, but anyway that's not the case in VB.
It prevents infliction of variables.
Basically a feature to give you at least one less possible error in your code.
Also it's a little step closer to the scoping of variables in more advanced languages.
'Option Explicit' can be "put" there by default by ticking the Tools >> Options... >> Require Variable Declaration option.:confused: ???Quote:
Originally Posted by joaquim
Variables are ALWAYS treated as Variants if declared liked this (if data type is not specified):On the other hand:Code:Dim x
That's got nothing to do with 'Option Explicit'. It just means that this:Code:Dim y As someType 'Integer, Long, String...
... will throw an error (if 'Option Explicit' is used and 'x' is not declared).Code:Private Sub Form_Load()
x = 105
End Sub
Btw... making your var Public and putting it into a Module will allow you to access it from any point in your Project.
look these:Quote:
Originally Posted by papafrancisco
if you declare a variable in declare position like this:
these variable will use in every place in that form, module, class... nothing more...Code:dim varname as vartype
if you declare in these way in same position:
these variable will be used in every project...Code:public varname as vartype
but if you use in a procedure or a function you can only use it there...
with other words:
the dim keyword is use for local variable, but public is for global variable.
i hope help you
the "Require Variable Declaration option" or "'Option Explicit" is for detect if the variable was declared or not and if it's not then gives you an error...Quote:
Originally Posted by gavio
if you don't give a vartype to a variable, then the vartype will be variant
I don't know if it's true, but I think what was meant is;Quote:
Originally Posted by gavio
If you don't use Option Explicit, and you run your code, you will note that x is declared as variant
exactly, what you said, joaquim ^
Look at this simple code:
Public Sub Command1_Click()
If test = "1" Then
x = 1
End If
End Sub
Public Sub Text1_Change()
Dim test As String
test = Text1.Text
End Sub
x will always = 0 because test = empty in the sub command_click. What am I missing here for test to equal what was entered via the textbox?
you should define test above the rest of the code
example:
But a more efficient version of this achievement is:Code:Public test As String '// Might as well be dim if you only us it in this form
Private Sub Command1_Click()
If test = "1" Then
x = 1
End If
End Sub
Private Sub Text1_Change()
test = Text1.Text
End Sub
Code:Private Sub Command1_Click()
If Text1.Text = "1" Then
x = 1
End If
End Sub
uhmm... I added the line of code you suggested however now the variabe test = "" under Sub Command_Click . Guess I still haven't grasp this yet...:confused:
Public test As String
Public Sub Command1_Click()
rer:
If test = "1" Then
x = 0
End If
End Sub
Public Sub Text1_Change()
Dim test As String
test = Text1.Text
End Sub
If I've missed this point somewhere in this thread then I apologize for repeating it.
Somehow, the paramount benefit of using Option Explicit was not stated! Consider the following code as a classic example of how Option Explicit will save you much aggravation, especially as your projects grow!:
Code:' This Option Explicit demo contains an intentional error used to demonstrate the
' advantage of explicit declarations. Put two CommandButtons & two TextBoxes on your
' Form and run the code and click Command1 and then Command2. You should receive an
' error "Variable not defined". VB will highlight the error object for you. Now Rem
' Option Explicit and repeat the previous steps. When you click Command2, nothing
' will happen, demonstrating that Option Explicit IS our friend! In a large app a typo can
' take forever to find.
Option Explicit ' Rem (') this and see what happens!
Dim strMyText As String ' Declare variable
Private Sub Command1_Click()
Text1.Text = strMyText
End Sub
Private Sub Form_Load()
strMyText = "Option Explicit is our friend" 'Initialize variable
End Sub
Private Sub Command2_Click()
Text2.Text = stMyText ' Variable has a typo!
End Sub
Let me try a different approach. Entering a 1 into the procedure Sub Text1_Change how can I have the sub Command1_Click see the variable test as being equal to 1? Or in other words how can I make a variable available to all procedures?
Public Sub Command1_Click()
If test = "1" Then
x = 1
End If
End Sub
Public Sub Text1_Change()
Dim test As String
test = Text1.Text
End Sub
[QUOTE=papafrancisco] how can I make a variable available to all procedures?
Declare it in the Declarations section as I did in my Option Explicit demo (post 15) .
Code:Option Explicit
Dim strTest As String
Dim intX As Integer
Private Sub Command1_Click()
strTest = Text1.Text
If strTest = "1" Then
intX = 1
MsgBox "intX = " & intX
End If
End Sub
Quote:
Originally Posted by papafrancisco
Code:Dim test As String '// It's supposed to be here, above all functions and subs
Public Sub Command1_Click()
If test = "1" Then
x = 1
End If
End Sub
Public Sub Text1_Change()
Dim test As String '// This line doesn't go here!
test = Text1.Text
End Sub
Why keep the Text1_Change sub? In the poster's code it really doesn't do anything. I refer to post 18 where it's eliminated altogether.
You're right CDrive. That line of code does nothing. I just created a simple code to express/convey what I was asking.
TheBigB... That's the ticket! Thanks to all for the help. This is one of the best forums!