9 Replies - 1118 Views - Last Post: 09 February 2012 - 07:59 PM Rate Topic: -----

#1 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

VB.Net if statement

Posted 07 February 2012 - 11:07 PM

I am having an issue with this part of my code. I need this code to catch letters and make sure the numbers are with a range of 1 - 10000. I have everything in and kind of working.

Only issue is when the if statement catches a letter is sends the message box but it continues to tell me a string can not be converted to double. This worked fine until I added the ElseIf to make it look and work better.

Added statements where my issue is.

        'Setting a numbers only catch.
        If IsNumeric(AmountBox1.Text) = False Then
            'Amount must be numeric. Please enter a valid loan amount.
            MsgBox("Ok, no letters or characters, just use numbers! Please enter a valid amount between 1 and 10,000.")
            'tried an and here but get a syntax error.
            And terminate program
            'Setting a lessthan 1 catch.
        ElseIf AmountBox1.Text <= 0 Then
            'Amount must be above 1.
            MsgBox("Please use amounts no lower than 1")
            'Setting a greaterthan 10000 catch.
        ElseIf AmountBox1.Text >= 10001 Then
            'Amount must be below 10000.
            MsgBox("Please use amounts no higher than 10000")
        End If

'Here is the catch the error: Conversion from string "any letter within the 'amount" to type double. I understand what it is saying but do not know how to 'stop it from going to this line. Need it to stop at the if statement.      

                                 
 Amount = AmountBox1.Text


This post has been edited by FinFin: 07 February 2012 - 11:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net if statement

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: VB.Net if statement

Posted 07 February 2012 - 11:15 PM

try converting the Textbox input to double,

CDbl(AmountBox1.Text)

Was This Post Helpful? 1
  • +
  • -

#3 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: VB.Net if statement

Posted 07 February 2012 - 11:24 PM

Sorry forgot to add amount is already a double
 Dim Amount as Double


I need it to stop at this if statement if it is false,
 
            If IsNumeric(AmountBox1.Text) = False Then
            'Amount must be numeric. Please enter a valid loan amount.
            MsgBox("Ok, no letters or characters, just use numbers! Please enter a valid amount     between 1 and 10,000.")


When the code catches something other than a number here it sends the message box, once the user clicks ok the program continues to this code
 Amount = AmountBox1.Text 

and gives me the error message.

How do I stop the program from going on to this?
 Amount = AmountBox1.Text 

Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: VB.Net if statement

Posted 07 February 2012 - 11:34 PM

use Exit Sub after the message to stop that from happening.

Messagebox.show("Ok, no letters or characters, just use numbers! Please enter a valid amount between 1 and 10,000.")
Exit Sub

Was This Post Helpful? 1
  • +
  • -

#5 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: VB.Net if statement

Posted 07 February 2012 - 11:54 PM

Sweet thank you very much, that was too easy.

Hate being a newbee!

Now all I have to do is write the rest and I will be good. Thanks again for the push!!

Fin
Was This Post Helpful? 0
  • +
  • -

#6 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: VB.Net if statement

Posted 09 February 2012 - 04:26 PM

View PostnK0de, on 07 February 2012 - 11:15 PM, said:

try converting the Textbox input to double,

CDbl(AmountBox1.Text)


Asking here as this was suggested here and did not want to start a new thread, sorry if I was wrong.

Ok, 1 more question about this comment,

Is there a way to convert the DropDownBox to decimal?

Like
 CDml(AmountBox1.Text)

Would that work? Basically what is the Cdbl code for decimal?

Ok, found the answer it is
CDec(AmountBox1.Text)


Now it is saying Expression is not a method. How do I fix this. I need the box to be converted to decimal?

This post has been edited by FinFin: 09 February 2012 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 jurdendurden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 01-November 07

Re: VB.Net if statement

Posted 09 February 2012 - 06:27 PM

Try:

Dim d As Decimal = AmountBox1.Text


Was This Post Helpful? 1
  • +
  • -

#8 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: VB.Net if statement

Posted 09 February 2012 - 07:14 PM

try,

Dim amount As Decimal

amount = Decimal.Parse(AmountBox1.Text)

This post has been edited by nK0de: 09 February 2012 - 07:15 PM

Was This Post Helpful? 1
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: VB.Net if statement

Posted 09 February 2012 - 07:27 PM

View Postjurdendurden, on 10 February 2012 - 07:12 AM, said:

Try:

Dim d As Decimal = AmountBox1.Text


Text box text property returns String, it is a bad practice to just assign string to numeric value. You have to parse them, and if you need to check if a parse was successful or not, you can use TypeParse() which returns boolean and you can use it in your condition.
Was This Post Helpful? 2
  • +
  • -

#10 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: VB.Net if statement

Posted 09 February 2012 - 07:59 PM

Thanks everyone for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1