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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MessageBox.Show on no input in Label?

Status
Not open for further replies.

RoccoSiffredi

Technical User
Nov 10, 2010
16
GB
I want the messagebox.show to display an error message if there is no input in a label :

If (txtdays.Text > 100) And (txtdays.Text = "") Then MessageBox.Show("Please enter a value between 1 and 100")

There are no errors in the code but if there is no input in the txtdays label VBE hangs. What am I doing wrong?
 

Code:
If (txtdays.Text > 100) And (txtdays.Text = "") Then
This statement can never be true: you will never have both conditions met. You can NOT have a value in your text box of 250 and the text box be ampty at the same time. :)

Try:
Code:
If ([blue]Val[/blue](txtdays.Text) > 100) [blue]Or[/blue] (txtdays.Text = "") Then


Have fun.

---- Andy
 

What do you measn by 'sill hangs'?

One simple Form, text box named txtdays and a command button:
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        If (Val(txtdays.Text) > 100) Or (txtdays.Text = "") Then
            MessageBox.Show("Please enter a value between 1 and 100")
        End If
    End Sub
End Class

VB.NET 2008, works like a dream :)

Have fun.

---- Andy
 
Thanks Andy. I created a new form with your code and it works. What I want to do is give errors if :

There is no input
There is not a numerical input
The value entered is over 100

For some reason this works but I don't understand why :

If Not (IsNumeric(txtdays.Text) > 100) Or txtdays.Text = "" Then

Can someone explain why that works and this doesn't? :

If Not (IsNumeric(txtdays.Text) Or txtdays.Text = "" Or txtdays.Text > 100 Then
MessageBox.Show("Please enter a value between 1 and 100")
End If
 
Actually it doesn't work, it produces errors even if the value entered is below 100.
 

Well, you have to be more specific.

If you say "Actually it doesn't work" - you need to specify what is it, which piece of code does not work?

Have fun.

---- Andy
 

This code works like a dream (again)
Code:
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
[blue]
        If Not (IsNumeric(txtdays.Text)) Or _
        txtdays.Text = "" Or _
        Val(txtdays.Text) > 100 Then
            MessageBox.Show("Please enter a value between 1 and 100")
        End If
[/blue]
    End Sub
End Class

Have fun.

---- Andy
 

If you are talking about his code:
Code:
If [red]Not[/red] ([blue]IsNumeric(txtdays.Text)[/blue] > 100) Or txtdays.Text = "" Then
then you need to know a little about the IF-THEN statement.

If you type 25 in your txtdays text box, first it is evaluated IsNumeric (blue portion of the code), which it is - TRUE. Then you change it to FALSE by the Not operator (red code). Then it does not latter if [tt]txtdays.Text = ""[/tt] is TRUE or FALSE because the entire statement will be FALSE anyway.

Have fun.

---- Andy
 
Yep that did it. The issue was I had this after my dims :

days = txtdays.Text
months = months.Text
years = text.Text

In order to add the three values together. I moved them after the validation and it worked.

Thank you very much. Can you explain what 'Val' does? Haven't come across this before, only been learning the langue a short time.
 

Sure,

If you type 25 in your text box, it is a text, not a number. Your computer sees it as "25", the same way as "abc" or "99 botles of beer on the wall".

[tt]Val()[/tt] function converts "25" text to a value of 25 - a number, so you can compare it to another number.

Have fun.

---- Andy
 
I'm a little late to the game, but one thing to note this is wrong:
Code:
IsNumeric(txtdays.Text) > 100

IsNumeric returns True (-1) and False (0) so it will always be less than 100.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Can someone recommend a VBA book or resource listing commands and their parameters?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top