Making Textboxes Process Integers

xer0syk0
08-13-2005, 10:26 AM
Whenever i try to process any data from a textbox as an integer, it always becomes 0. For example:

Option Explicit
Dim Req1 As Integer
Dim Cur2 As Integer
Dim Num1 As Integer
Dim Number2 As Integer
Dim Rawr As Integer

Private Sub cmdProcess_Click()
Rawr = 149
txtReq1.Text = Req1
txtCur2.Text = Cur2
Num1 = ReqExp - CurExp
Num2 = Number / (Rawr * 2)
Text1.Text = Number2 + "" + "is the magical number."
End Sub

So...when people enter a number into Req1, and into Req2, and they hit the Process button, all values automatically become 0.
It produces a "Type mismatch" error. In debug, the value of Req1 = 0, and Cur2 = 0, and everything else, also = 0.
How do i correct this?
Can anyone help me?

Thanks.

LaVolpe
08-13-2005, 10:34 AM
Use VB's Val() function & optionally the CInt() one also:
CInt(Val(textboxname.text))
Suggest looking up help on those to answer any subsequent questions that come to mind.

TeraBlight
08-13-2005, 09:20 PM
I think the basic problem with your code is that you are using the assignment the wrong way 'round:

Option Explicit
Dim Req1 As Integer
Dim Cur2 As Integer
Dim Num1 As Integer
Dim Number2 As Integer
Dim Rawr As Integer

Private Sub cmdProcess_Click()
Rawr = 149
txtReq1.Text = Req1 'this puts Req1 into a textbox
txtCur2.Text = Cur2 'this puts Cur2 into another textbox
Num1 = ReqExp - CurExp 'I don't know if these are more variables or functions
Num2 = Number / (Rawr * 2) 'again, I have no idea what "Number" is
Text1.Text = Number2 + "" + "is the magical number." 'nothing has been assigned to Number2
End Sub

If you weren't using Option Explicit already, I would tell you to do so. But you are, so either you have a lot more code than you are showing us or this will produce a LOT of compile errors because you don't use the variables you are declaring. VB is "dumb", it won't "guess" that you might mean "Num2" when you type "Number2", and so on.

On a sidenote, you should declare your variables at procedure, not module, level unless this is actually necessary. IF it is necessary, declare them using "Private" or "Public" rather than "Dim". Furthermore, try to make your names a little more descriptive than "Num2" :)
I think studying our Introductory Standards And Practices Tutorial would be very much worthwhile!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum