|
-
May 16th, 2008, 04:28 PM
#1
Thread Starter
Member
[RESOLVED] Embarass to Ask but Here Goes
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?
-
May 16th, 2008, 04:36 PM
#2
Re: Embarass to Ask but Here Goes
Declare it modular, at the top, not inside the Sub.
Code:
Option Explicit
Private YourVarName as Something
...
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.
-
May 16th, 2008, 04:46 PM
#3
Thread Starter
Member
Re: Embarass to Ask but Here Goes
Thanks jcis for the response. By using the option explicit do I have to initially declare all variables?
-
May 16th, 2008, 05:04 PM
#4
PowerPoster
Re: Embarass to Ask but Here Goes
 Originally Posted by papafrancisco
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...
if you don't use it the variables used will be variant type...
-
May 16th, 2008, 05:10 PM
#5
Thread Starter
Member
Re: Embarass to Ask but Here Goes
Maybe I'm dreaming but didn't the older quick basic allowed a variable to be used or tested anywhere?
-
May 16th, 2008, 05:22 PM
#6
Re: Embarass to Ask but Here Goes
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.
Last edited by TheBigB; May 16th, 2008 at 05:30 PM.
Delete it. They just clutter threads anyway.
-
May 16th, 2008, 05:23 PM
#7
Re: Embarass to Ask but Here Goes
'Option Explicit' can be "put" there by default by ticking the Tools >> Options... >> Require Variable Declaration option.
 Originally Posted by joaquim
... if you don't use it the variables used will be variant type...
???
Variables are ALWAYS treated as Variants if declared liked this (if data type is not specified):On the other hand:
Code:
Dim y As someType 'Integer, Long, String...
That's got nothing to do with 'Option Explicit'. It just means that this:
Code:
Private Sub Form_Load()
x = 105
End Sub
... will throw an error (if 'Option Explicit' is used and 'x' is not declared).
-
May 16th, 2008, 05:25 PM
#8
Re: Embarass to Ask but Here Goes
Btw... making your var Public and putting it into a Module will allow you to access it from any point in your Project.
-
May 16th, 2008, 05:28 PM
#9
PowerPoster
Re: Embarass to Ask but Here Goes
 Originally Posted by papafrancisco
Maybe I'm dreaming but didn't the older quick basic allowed a variable to be used or tested anywhere?
look these:
if you declare a variable in declare position like this:
Code:
dim varname as vartype
these variable will use in every place in that form, module, class... nothing more...
if you declare in these way in same position:
Code:
public varname as vartype
these variable will be used in every project...
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
-
May 16th, 2008, 05:31 PM
#10
PowerPoster
Re: Embarass to Ask but Here Goes
 Originally Posted by gavio
'Option Explicit' can be "put" there by default by ticking the Tools >> Options... >> Require Variable Declaration option.  ???
Variables are ALWAYS treated as Variants if declared liked this (if data type is not specified): On the other hand:
Code:
Dim y As someType 'Integer, Long, String...
That's got nothing to do with 'Option Explicit'. It just means that this:
Code:
Private Sub Form_Load()
x = 105
End Sub
... will throw an error (if 'Option Explicit' is used and 'x' is not declared).
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...
if you don't give a vartype to a variable, then the vartype will be variant
-
May 16th, 2008, 05:34 PM
#11
Re: Embarass to Ask but Here Goes
 Originally Posted by gavio
 ???
I don't know if it's true, but I think what was meant is;
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 ^
Delete it. They just clutter threads anyway.
-
May 16th, 2008, 05:52 PM
#12
Thread Starter
Member
Re: Embarass to Ask but Here Goes
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?
-
May 16th, 2008, 05:57 PM
#13
Re: Embarass to Ask but Here Goes
you should define test above the rest of the code
example:
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
But a more efficient version of this achievement is:
Code:
Private Sub Command1_Click()
If Text1.Text = "1" Then
x = 1
End If
End Sub
Delete it. They just clutter threads anyway.
-
May 16th, 2008, 06:04 PM
#14
Thread Starter
Member
Re: Embarass to Ask but Here Goes
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...
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
-
May 16th, 2008, 07:43 PM
#15
Re: Embarass to Ask but Here Goes
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
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
May 16th, 2008, 08:23 PM
#16
Thread Starter
Member
Re: Embarass to Ask but Here Goes
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
-
May 16th, 2008, 09:09 PM
#17
Re: Embarass to Ask but Here Goes
[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) .
Last edited by CDRIVE; May 16th, 2008 at 09:22 PM.
Reason: Correction
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
May 16th, 2008, 09:41 PM
#18
Re: Embarass to Ask but Here Goes
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
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
May 17th, 2008, 03:42 AM
#19
Re: Embarass to Ask but Here Goes
 Originally Posted by papafrancisco
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
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
Delete it. They just clutter threads anyway.
-
May 17th, 2008, 07:53 AM
#20
Re: Embarass to Ask but Here Goes
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.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
May 17th, 2008, 07:59 AM
#21
Thread Starter
Member
Re: Embarass to Ask but Here Goes
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|