Solved

VB 2008  - Convert contents of TextBox to an Integer

Posted on 2009-07-12
3
306 Views
Last Modified: 2012-05-07
ActiveAns is a textbox.  It may contain Nothing or an integer.  If it contains a number I need to convert that to (n) as below.  I can't seem to do this without runtime errors that say it cannot convert it.  

How do I test ActiveAns and then convert the contents of ActiveAns to n ?


If IsNumeric(ActiveAns.Text) And ActiveAns.Text > 0 Then
    Dim n As Integer = Int(ActiveAns.Text)
    blah blah blah
End If

Open in new window

0
Comment
Question by:ljcor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 24833637
Use the Integer.TryParse method....

        Dim n As Integer
        If Integer.TryParse(ActiveAns.Text, n) Then
            MsgBox(n.ToString & " is an integer")
        Else
            MsgBox("is not an integer")
        End If

Wayne
0
 
LVL 2

Expert Comment

by:Moed
ID: 24834070
If IsNumeric(ActiveAns.Text) And ActiveAns.Text > 0 Then
try
    Dim n As Integer = Int(ActiveAns.Text)
    blah blah blah
catch ex as exception
Msgbox("Entry must be a number")
end try
End If

0
 

Author Closing Comment

by:ljcor
ID: 31602542
Thanks to each of you for your advice.  I didn't use the second solution because I thought I had used it earlier without a result.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question