user3242798
user3242798

Reputation: 19

how to use MSGBOX YES NO

i have the right codes for this. when i press yes the command will execute but when i press the NO the sub doesnt exit it still executes the command.. can someone help me?? im a newbee in vb programing.. (sorry for the english)

heres the code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim payed As Integer
        Dim balance As Integer
        Dim paying As Integer
        Dim d As Integer
        Dim c As Integer
        Try

            Dim ans As Integer
            If TextBox7.Text = "" Or 0 Then

                MsgBox("please enter amount", MsgBoxStyle.Critical)
            Else
                If TextBox6.Text = 0 Then

                    MsgBox("the student is fully payed", MsgBoxStyle.Critical)
                Else
                    ans = MsgBox(" are you want to upadate the students payment? ", MsgBoxStyle.YesNo, "confirm ")
                    If MsgBoxResult.Yes Then


                        payed = Val(TextBox5.Text)
                        balance = Val(TextBox6.Text)
                        paying = Val(TextBox7.Text)
                        d = payed + paying
                        c = balance - paying
                        TextBox5.Text = d
                        TextBox6.Text = c

                        Dim SqlQuery = "UPDATE sample SET [Payed Amount] ='" & TextBox5.Text & "',Balance ='" & TextBox6.Text & "' WHERE ID = " & a & " ; "
                        Dim sqlcommand As New OleDbCommand
                        With sqlcommand
                            .CommandText = SqlQuery
                            .Connection = conn
                            .ExecuteNonQuery()
                        End With
                        MsgBox("student payment succesfully updated")
                        My.Computer.Audio.Play(My.Resources.CASHREG, AudioPlayMode.Background)
                        If TextBox6.Text = 0 Then
                            MsgBox("the student is now fully paid", MsgBoxStyle.Information)
                            TextBox7.Text = ""
                        Else

                            MsgBox("students current balance is " & Convert.ToString(TextBox6.Text))
                            LoadListView()
                            TextBox7.Text = ""
                        End If
                    Else
                        Exit Sub
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Upvotes: 1

Views: 6440

Answers (1)

AakashM
AakashM

Reputation: 63340

This is part of your code:

ans = MsgBox(" are you want to upadate the students payment? ", MsgBoxStyle.YesNo, "confirm ")
If MsgBoxResult.Yes Then

You are assigning the result of the message box call to ans, but then you aren't actually testing the value of ans. When you say

If MsgBoxResult.Yes Then

this will always be true, because MsgBoxResult.Yes is a constant value which will always come out as True. You actually want this test to be

If ans = MsgBoxResult.Yes Then

I'm slightly surprised that what you have compiles, although VB.NET isn't my strong point. I would suggest you always use Option Strict in your code, which I believe would show this up as a problem at compile time.

Upvotes: 2

Related Questions