Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 27th, 2005, 09:49 PM
Registered User
 
Join Date: Dec 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Nested If Statement

I need help on the following code in VB 2005 express. In the following code, VB only executes the 'second IF' not the 'first IF' statement. I am not sure what I am doing wrong or it is a bug.


Thanks

KCTin

----------------------------------------------------------------

If IsNumeric(textbox1.Text) Then
   If Val(textbox1.Text) >= 0 Then
   Else
       MsgBox("Please enter valid number")
   End If
End If

----------------------------------------------------------------



 
Old December 28th, 2005, 12:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

What are you putting in the text box to test this and what event are you firing? This worked OK for me although I wouldn't use Val to check for numbers greater than 0.

Also, in order for the second If statement to run, the first one has evaluate to True. If you are getting a result from the second one, the first is executing.

J
 
Old December 28th, 2005, 01:10 PM
Registered User
 
Join Date: Dec 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am just using numeric value number 10 or 20. I also tried without Val in the IF statement.

I tried input Alphabets to see if the first IF statement will catch and It didn't

Try using alpahabets in your input and see if you get error message box

Thanks

KCTin



Quote:
quote:Originally posted by katsarosj
 What are you putting in the text box to test this and what event are you firing? This worked OK for me although I wouldn't use Val to check for numbers greater than 0.

Also, in order for the second If statement to run, the first one has evaluate to True. If you are getting a result from the second one, the first is executing.

J
 
Old December 28th, 2005, 01:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

What I meant about the Val function is that I would use something else like "Convert".

I think your problem is your logic. You have to look at the order of execution for your If statements. What you have:

-----------------

If IsNumeric(textbox1.Text) Then
   If Val(textbox1.Text) >= 0 Then
   Else
       MsgBox("Please enter valid number")
   End If
End If

-----------------

means that unless the textbox value is numeric, then it won't execute. So if you put in letters, it is false and nothing else is executed. Try this instead:

-----------------

        If IsNumeric(TextBox1.Text) Then
            If Convert.ToInt32(TextBox1.Text) >= 0 Then
                'do something here if the number is >= 0
                MessageBox.Show("Great! Your number was >= 0.")
            Else
                'do something here if the number is < 0
                MessageBox.Show("Your number was less than 0")
            End If
        Else
            MessageBox.Show("Please enter valid number")
        End If

-----------------

J





Similar Threads
Thread Thread Starter Forum Replies Last Post
nested for hastikeyvan Classic ASP Professional 1 July 27th, 2006 06:28 PM
how to write sql statement for nested subuery thas123 SQL Server 2000 3 February 23rd, 2006 02:37 PM
Nested IIF Statement Help Needed Corey Access 4 November 17th, 2005 10:19 AM
Nested Fetch statement cole SQL Language 0 May 11th, 2005 06:47 PM
Nested If pans Access 4 December 10th, 2004 12:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.