Cannot seem to make the message box to not appear when I click cancel. Help please !

        Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
            Dim input As String
            Dim total As Single
            Dim average As Single
            Dim count As Single

            Const msg1 As String = "Enter a sales amount. Click Cancel to end"
            Const msg2 As String = "Sales Entry"


            count = 1

            Do
                input = InputBox(msg1, msg2)


                If Not IsNumeric(input) Then
                    MessageBox.Show("Error, please check input")
                Else
                    count = count + 1
                    total = total + input

                End If



            Loop Until (IsNumeric(input) And count > 5)




            lblTotalSales.Text = total.ToString("C2")

            average = total / 5
            lblAverage.Text = average.ToString("C2")




        End Sub

There is another control flow needed before

If Not IsNumeric(input) Then

Your codes are quite right and you need some modification. Like

        Do

            input = InputBox(msg1, msg2)

            'Check is the variable stores any value or not
            If Not String.IsNullOrWhiteSpace(input) Then

                'If it stores something do the calculations
                If Not IsNumeric(input) Then
                    MessageBox.Show("Error, please check input")
                Else
                    count = count + 1
                    total = total + input
                End If
            Else
                'If variable input does not contain any value
                'Prompt to user, he is ready to continue or not
                Select Case MessageBox.Show("Do you sure to continue?", "Confirmation.....", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                    Case Windows.Forms.DialogResult.Yes
                        'If the user confirms to continue
                        'then nothing to do
                        Continue Do

                    Case Windows.Forms.DialogResult.No
                        'If the user desires to exit then exit from loop
                        Exit Do

                End Select
            End If
        Loop Until (IsNumeric(input) And count > 5)

An InputBox() always returns a Null Or Empty string when you click the cancel Button. So you must have to consider to make an extracontrol flow for empty string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.