Morning all,

I have some code which says
if a text box is empty show a msgbox asking user to input data into this textfield.

i then want only an O.K button to show on the msgbox but then no to continue with the sub.

If Me.txtLine2Rcon.Text = "" Then MsgBox("Please insert a running condition for Line 2 before you add it to the database", vbOKOnly)

        Dim NewLongDesc As String
        Dim RConv As String
        Dim StkCodeV As String

        RConv = Me.txtLine2Rcon.Text
        NewLongDesc = Me.txtLongDesc.Text
        StkCodeV = Me.txtStockCode.Text

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Try
            con.ConnectionString = "Data Source=10.10.0.25;Initial Catalog=RConditions;Persist Security Info=True;User ID=mbish;Password=mbish"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO LN2(Stkcode, LongDesc,RCon) VALUES(@p1,@p2,@p3)"
            cmd.Parameters.AddWithValue("@p1", StkCodeV)
            cmd.Parameters.AddWithValue("@p2", NewLongDesc)
            cmd.Parameters.AddWithValue("@p3", RConv)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
        Finally
            con.Close()
        End Try

        MsgBox("Record Added")
        Me.line2add.Enabled = False
        If Me.txtLine2Rcon.Text = "" Then Line2V = True
        Me.line2add.BackColor = Color.Red

at the moment when there is no data in the txtbox you get the msgbox but then it runs theough all the code

Recommended Answers

All 10 Replies

If Me.txtLine2Rcon.Text = "" Then
 MsgBox("Please insert a running condition for Line 2 before you add it to the database", vbOKOnly)
return
end if
Dim NewLongDesc As String
...

when using the above, i get an error end if must be matched with if

commented: don't you know the basic IF ...END IF syntax ? -3

yeah coz you are not copying my code. guess u still keep the " If Me.txtLine2Rcon.Text = "" Then MsgBox("Please insert a running condition for Line 2 before you add it to the database", vbOKOnly)
in one line....

copy it as i wrote it and it will work.

Hi,

You can try something like this:

If Me.txtLine2Rcon.Text = "" Then MsgBox("Please insert a running condition for Line 2 before you add it to the database", vbOKOnly)
        End Sub
else
        Dim NewLongDesc As String
        Dim RConv As String
        Dim StkCodeV As String
        ' rest of your code
if me.textbox.text.length = 0 then
msgbox("BAD", vbokonly)
exit sub
end if

i dont know if it matters in vb.net, but i know in vb6 if you used len() or lenb() to determine if a string was empty, it was a lot faster then testing it against = ""

not sure if it still is efficient, but its a habbit that i picked up and carried over to the .net framework

If Me.txtLine2Rcon.Text = "" Then MsgBox("Please insert a running condition for Line 2 before you add it to the database", vbOKOnly)
exit sub 'this is to leave the sub procedure and prevent further code execution

oops, wrong thread

Syntax: MsgBox("Msg",MsgBoxStyle.OkOnly,"Msg Title")
now need to do at your own.

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.