error while checking textbox data...
Well..this may be a very noob question but I just cant figure it out...well, the thing is that I am making a sum and then you've got to input the right sum in a textbox. After that, you click a button and it tells you if its right or wrong in a textbox...
I used :
Private Sub ansbtn_Click()
If ans.Text = m + n Then 'textbox in which you input the result of m and n
MsgBox "Correct" ' if it is correct then this message box should comeout
resultsadd.resultslbl.Caption = Val(resultsadd.resultslbl.Caption) + 1 ' this adds 1 to the final results
Else
MsgBox "Wrong" ' If it is wrong, this message box should be displayed
End If
Addition1.Hide ' after pressing the button, then the form hides
Addition2.Show ' another form shows up...
End Sub
The thing is that when I input the number and click the button, the wrong message comes out but the answer is correct... I dont know what happens... can anybody help me? thnx...
[1062 byte] By [
limpit] at [2007-11-19 14:07:23]

# 1 Re: error while checking textbox data...
Do you have "Option Explicit" define at the top of your code (i.e. is the "Require Variable Declaration" setting set)?
If not then chances are that that is the problem.
Also, try this step through your code (or put a break point in it). On the line "If ans.Text = m + n Then" make sure that you are getting the correct values in m, n and ans.text.
# 2 Re: error while checking textbox data...
Well, what does option explicit do exactly? When I type Option explicit at the top, then vb tells me that the n and m variables are not defined in the part that says :
Private Sub Form_Load()
Call Randomize
n = Int((10 * Rnd) + 1)
m = Int((10 * Rnd) + 1)
add1.Caption = m
add2.Caption = n
End Sub
any ideas pink?? Please help me...
limpit at 2007-11-9 20:29:52 >

# 3 Re: error while checking textbox data...
You need to define the variables
Option explicit
Dim n, m as integer 'declared for the entire form, not just sub Form_Load
Private Sub Form_Load()
Call Randomize
n = Int((10 * Rnd) + 1)
m = Int((10 * Rnd) + 1)
add1.Caption = m
add2.Caption = n
End Sub
What was happening was this:
You didn't declare n and m. When you called sub Form_Load, they were created with a scope (lifespan and accessibility) of that procedure. When Form_Load ended, they vanished.
Later, when you again tried to use n and m, they were created again (completely different brand new versions of n and m) within the scope of that other procedure (the button click event probably), and since they'd just been created, they would have had a value of 0.
I'd recommend that you always always always use Option Explicit.
Option Explicit tells VB not to create a variable that you have used without defining. It's a sloppy practice to let VB do that, and it can create a lot of hidden problems.
For example:
Dim myvar as integer
myvar=5
msgbox myvra
Will, without Option Explicit, give you a message box with a 0 or "empty" or something like that in it. This happens because my "typo" created a new variable. If you use Option Explicit, VB will stop and say "I don't know what this is!" Then you'll have a chance to recognize your mistake and fix it.
Also I believe that if you don't specify a type by declaring the variable, it creates a variant which is slower, and can be assigned anything (string, integer, double, objects) which may lead to even more errors.
# 4 Re: error while checking textbox data...
thnx thnx thnx!!! thnx a lot... now i can finally finish...You really helped me out...
limpit at 2007-11-9 20:31:55 >

# 5 Re: error while checking textbox data...
Hi mishali,
I want to edit some of lines !!
Private sub Proc1()
Dim myvar as integer
myvar=5
end Sub
Private Sub Proc2()
msgbox myvar
End sub
In Only this condition without Option Explicit, give you a message box with a 0 or "empty" or something like that in it. This happens because The Variable scope is lost in Proc1 and there is no value for MyVar in Proc2.
# 6 Re: error while checking textbox data...
Yip thought it woulb be that.
Without "Option Explicit" you can use variables without having to define them so we can write:
a = 2
b = 8
c = a + b
and there we go, everything is fine and it all works and c = 10. BUT, now consider what happens when you make a spelling mistake!!
myFirstNumber = 2
mySecondNumber = 8
myThirdNumber = myFistNumber + mySecondNumber
(Notice the myFirstNumber is spelt incorrectly)
Now the compiler thinks that "myFistNumber" is a new variable, so it defines it and assigns it a value of 0. You can hunt for days trying to work out why your program isn't working.
Much the same thing is happening to you. You probably have these two variables m and n which you set some where (in another procedure). But the compiler only defined them for the scope of that procedure. Now in this buttonc click procedure it re-defines the variables m and n, but has no idea they were meant to refer to a previous variables m and n.
You need to place the following line into you program at the top (as Mishlai said)
Dim n, m as integer 'declared for the entire form, not just sub Form_Load
I would also recomend going into "Options" and checking the "Require Variable Declaration" setting. This places Option Explicit at the top of all of your code, so it should help prevent this from happenning again.
# 7 Re: error while checking textbox data...
any ideas pink?? BTW: its PINKY as in the mouse... not pink as in a rather happy colour.
# 8 Re: error while checking textbox data...
Dim n, m as integer
Before limpit learns some bad programming habits, the above code only declares m as an integer; n is undefined and VB will delare it as a Variant.
Variants are generally undesirable and should only be used in special cirumstances, when a programmer is uncertain what data type is going to be handled, for example.
You must define the type of each variable when listing variables on one line like this
dim n as integer, m as integer
...
Dan_H at 2007-11-9 20:35:59 >

# 9 Re: error while checking textbox data...
Very true.
Shot for pointing that out, Dan_H.