Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vb2008 - For / Next Question using Try / Catch 2

Status
Not open for further replies.

emaduddeen

Programmer
Mar 22, 2007
184
US
Hi Everyone,

In this procedure I have a "try" / "catch" statement. Is it possible to not execute the "next" statement if there is an error?

Here's my code. It's based on a lesson from the MS Press "Step by Step" book. I just wanted to add some extra code to it:

Code:
    Private Sub btnEnterTemps_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnterTemps.Click
        Dim Prompt As String
        Dim Title As String
        Prompt = "Enter today's high temperature."

        ' Load the temperatures into an array.
        '-------------------------------------
        For TheLoopIndex = 0 To UBound(Temperatures)
            Title = "Day " & (TheLoopIndex + 1)
            Try
                Temperatures(TheLoopIndex) = InputBox(Prompt, Title)
            Catch
                MsgBox("Please enter a number.", , "OOOPPPSSS!!!!!")
            End Try
        Next
    End Sub

I'm looking to stop the "next" statement from executing after the message box is displayed.

Thanks.

Truly,
Emad
 
Is this what you mean?
Code:
    Private Sub btnEnterTemps_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnterTemps.Click
        Dim Prompt As String
        Dim Title As String
        Prompt = "Enter today's high temperature."

        ' Load the temperatures into an array.
        '-------------------------------------
        For TheLoopIndex = 0 To UBound(Temperatures)
            Title = "Day " & (TheLoopIndex + 1)
            Try
                Temperatures(TheLoopIndex) = InputBox(Prompt, Title)
            Catch
                MsgBox("Please enter a number.", , "OOOPPPSSS!!!!!")
            [COLOR=#ff0000][b]Exit For[/b][/color]
            End Try
        Next
    End Sub
 
Hi,

Thanks for the fast replies.

I actually want to stay in the for loop but not increment the loop counter.

Example:
User enters 10 for day 1, 20 for day 2 and makes an error and enters ABC for day 3. I would like the loop to stay at day 3 until the user enters a number.

Truly,
Emad
 
There isn't any way to keep the For/Next loop from incrementing. You'll need to use a Do loop and control the array index yourself based on whether or not the input was valid:
Code:
        Dim dayIndex As Integer = 0
        Do While (dayIndex <= Temperatures.GetUpperBound(0))
            Try
                Temperatures(dayIndex) = InputBox("Enter today's high temperature", "Day " & (dayIndex + 1).ToString)
                dayIndex += 1
            Catch ex As Exception
                MsgBox("Please enter a number.", , "OOOPPPSSS!!!!!")
            End Try
        Loop
 

How about:
Code:
    Private Sub btnEnterTemps_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnterTemps.Click
        Dim Prompt As String
        Dim Title As String
        Prompt = "Enter today's high temperature."

        ' Load the temperatures into an array.
        '-------------------------------------
        For TheLoopIndex = 0 To UBound(Temperatures)
            Title = "Day " & (TheLoopIndex + 1)
            Try
                Temperatures(TheLoopIndex) = InputBox(Prompt, Title)
            Catch
                MsgBox("Please enter a number.", , "OOOPPPSSS!!!!!")
                [blue]TheLoopIndex = TheLoopIndex - 1[/blue]
            End Try
        Next
    End Sub

Have fun.

---- Andy
 
Hi Andy,

Perfect. It works well. :)

Glad you can change the loop counter on the fly and just one line of code at that.

Truly,
Emad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top