how to check whether one out of eight textboxes is empty or not

Currently I am doing my project on a simple inventory system using visual basic.NET 2003. I encountered a problem which i have no idea how to solve it. I have eight textboxes in my form and how am i going to check whether anyone out of the eight textboxes is empty ?

I have use if statement but the statement seems a bit longer. So i hope someone can teach a shorter way of doing this.

By the way, is VB.NET 2003 works well with Microsoft Access 2007 ?

Thank you.

Comments

  • Please check -" Is Nothing ".... like wise.

    If RTrim(TextBox1.Text) = Nothing Then
    MsgBox("empty")
    Else
    MsgBox("Not Empty")
    End If

    May be this can helps you
  • I have try your code but it dint work the way i wanted. Anyway, thanks for your help.

    * I have mentioned in my previous post that I have 8 textboxes in my form so I want the program to check the textboxes whether they are empty or not, with a shorter and simple way compared to using if statement which is much longer.
  • seancampbellseancampbell Pennsylvania, USA
    If your 8 textboxes are named Textbox1 - Textbox8 then your code would look like this:

    [code]
    If TextBox1.Text.Length = 0 Or TextBox2.Text.Length = 0 Or TextBox3.Text.Length = 0 Or TextBox4.Text.Length = 0 Or TextBox5.Text.Length = 0 Or TextBox6.Text.Length = 0 Or TextBox7.Text.Length = 0 Or TextBox8.Text.Length = 0 Then
    MsgBox("One of the text boxes was empty")
    Else
    MsgBox("None of the text boxes were empty")
    End If
    [/code]

    If you need to know which one was empty then you would do it a bit differently. Typically, when I am validating the entry on a form, I give a specific message for the data that was missing. In those cases I use a function and if statement like this:

    [code]
    Function ValForm() as Boolean
    If TextBox1.Text.Length = 0 Then
    MsgBox("TextBox1 was Empty. Please enter data into TextBox1")
    Return False
    Else If TextBox2.Text.Length = 0 Then
    MsgBox("TextBox2 was Empty. Please enter data into TextBox2")
    Return False
    Else If TextBox3.Text.Length = 0 Then
    MsgBox("TextBox3 was Empty. Please enter data into TextBox3")
    Return False
    Else If TextBox4.Text.Length = 0 Then
    MsgBox("TextBox4 was Empty. Please enter data into TextBox4")
    Return False
    Else If TextBox5.Text.Length = 0 Then
    MsgBox("TextBox5 was Empty. Please enter data into TextBox5")
    Return False
    Else If TextBox6.Text.Length = 0 Then
    MsgBox("TextBox6 was Empty. Please enter data into TextBox6")
    Return False
    Else If TextBox7.Text.Length = 0 Then
    MsgBox("TextBox7 was Empty. Please enter data into TextBox7")
    Return False
    Else If TextBox8.Text.Length = 0 Then
    MsgBox("TextBox8 was Empty. Please enter data into TextBox8")
    Return False
    End If
    Return True
    End Sub

    Private Sub Button1_Click(ByVal sender, ByVal e) Handles Button1.Click
    If ValForm = True then
    'This case will happen when every text box has data in it
    'If ValForm finds a textbox with no text, it will message
    'the user and return false
    end
    End Sub
    [/code]

    I didn't verify that the code was spelled correctly with VS, but I think this will suffice for you to understand.

    Hope it helps
  • Thanks for your help. I have another question :

    How am I going to check for the validity of data entered by the users to ensure that the data enter matched the fields ? If the field is item price, how to make sure that the user only enter the specific data and nothing else ?




  • seancampbellseancampbell Pennsylvania, USA
    If your checking the Validity of inputted data, and want to verify that numbers were entered you can use the following method:
    [code]
    Dim A as Double 'This is a decimal number (floating point)
    If isNumeric(Textbox1.Text) = True Then
    'A = TextBox1.Text 'This works, but I prefer to do the casting myself
    A = CDbl(TextBox1.Text)
    Else
    MsgBox("Yo son, you need to enter a number, yo")
    End If
    [/code]
    If you want to make sure it is an Integer:
    [code]
    If isNumeric(TextBox1.Text) = True Then
    If CDbl(TextBox1.Text) = CInt(TextBox1.Text) Then
    'It is a number, and an integer
    Else
    MsgBox("Enter a whole number you whack individual.")
    End If
    Else
    'It was not a number
    MsgBox("Enter a whole number, you didn't even enter a number... yo")
    End If
    [/code]
    And finally, you think the person might have entered a Dollar sign or space on the number, you can remove trailing characters pretty simply like this:
    [code]
    Dim FilteredStr as String = TextBox1.Text
    FilteredStr = FilteredStr.Trim("$") 'This removes trailing dollar signs from either side of the string
    FilteredStr = FilteredStr.Trim() 'The default trim removes trailing spaces from either side
    If isNumeric(FilteredStr) = False Then
    MsgBox("I guess we are not that cool" & vbNewLine & FilteredStr)
    End If
    [/code]
  • Ya it works. Thanks for your help.
  • [code]You can use the following code to chek if a textbox is null. Enjooooy!!

    private sub CheckMyTextBox()
    Dim ct As Control
    Dim flag As Boolean

    flag = False
    For Each ct In Controls
    If TypeOf ct Is TextBox Then
    If ct = "" Then
    ct.BackColor = vbYellow
    flag = True
    End If
    End If
    Next
    If flag Then
    MsgBox "Please enter requireed fields.", vbExclamation, ""
    End If
    End Sub[/color][/code]
  • [code]You can use the following code to chek if a textbox is null. Enjooooy!!

    private sub CheckMyTextBox()
    Dim ct As Control
    Dim flag As Boolean

    flag = False
    For Each ct In Controls
    If TypeOf ct Is TextBox Then
    If ct = "" Then
    ct.BackColor = vbYellow
    flag = True
    End If
    End If
    Next
    If flag Then
    MsgBox "Please enter requireed fields.", vbExclamation, ""
    End If
    End Sub[/color][/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories