check for numeric value function (easy point)

hi there,
i need to create a function that will take the value of a text box and check if it is a numeric field or not.
if it is number then no problem, but if on the other hand is text then warm the user.
i have something like this but it gives me the message box twice?
any help would be appreciated.

 Public Shared Function NumberField(ByVal Extension As String) As String

        Dim number As String
        If IsNumeric(Extension) = True Then
            number = Extension
        Else
            number = MsgBox("you must enter a numeric value.", "Entry Error")
        End If
        Return number
    End Function
LVL 1
jsctechyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arthur_WoodCommented:
try this change:

Public Shared Function NumberField(ByVal Extension As String) As String

        Dim number As String
        If IsNumeric(Extension) = True Then
            number = Extension
        Else
            MessageBox.Show("You must enter a numeric value.")
            number = "-999"
        End If
        Return number
    End Function
 
Authored by: jsctechy
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
"but it gives me the message box twice?"

I don't see any reason why you would get two MsgBoxes...

Perhaps you have used AddHandler() AND ALSO have a Handles clause on the same sub?
0
jsctechyAuthor Commented:
i'm still getting the message box twice here is how i call it:

If Tools.NumberField(txtExtension.Text) = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jsctechyAuthor Commented:
actually i'm calling the function twice to validate the same number so how can i do to by pass this error and check the number only once?
do i have to assign it to a value and use that instead?
If Tools.NumberField(txtExtension.Text) = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If

        'Search for Dates and extension
        If Tools.NumberField(txtExtension.Text) <> "" Then
            If cboBranch.Text = "" Then
                If txtAreaCode.Text = "" Then
                    If txtExchange.Text = "" Then
                        If txtNumber.Text = "" Then
                            MsgBox("You are looking for Dates and extension only", MsgBoxStyle.Information, "Date & Extension Search Only")
                        End If
                    End If
                End If
            End If
        End If
0
jsctechyAuthor Commented:
ok i fix it like this:
        Dim Extension As String
        Extension = Tools.NumberField(txtExtension.Text)

        If Extension = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If

        'Search for Dates and extension
        If Extension <> "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates and extension only", MsgBoxStyle.Information, "Date & Extension Search Only")
        End If

but i want that if is not a numeric value then don't run the message box at all, how do i do this? and instead go back to the text box and set the focus and dont let you moved from there untill you have satisfied the requiered

Thanks
0
AutoeformsCommented:
i could be way off base here but looking out from a different angle. why do you need the error. make your life easy by only allowing them to enter numbers.

 use the masked edit control
or add the following to your key press event to you text box

        If IsNumeric(e.KeyChar) = False Then
            e.Handled = True
        End If

      or e.handled = ( e.Handled = True)
   
good luck
greg

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Arthur_WoodCommented:
this code:

If Tools.NumberField(txtExtension.Text) = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If

will generate two message boxesif the txtExtension is not a number, and here is how:

The first time come from the NumberField function, since the field is not a number, and you code, as wriiten (not my chane), will generate the MsgBox, and return a "0" - the Value of the MsgBox function (yes, MsgBox is a FUNCTION, and it returns the value of the Key that the user clicked, to close the MsgBox that was displayed), so then in the above code, the initial test (which presented the first messgeBox), fails (ince the value returned by your function is NOT "", and then, if each of the other tests faile (becuase those fields are NOT ""), then the second messagebox will be displayed.

Set a Breakpoint on the first line

If Tools.NumberField(txtExtension.Text) = "" Then
 

and then single-step through the code to see what is actually occuring, and why you get two message boxes.

AW
0
jsctechyAuthor Commented:
here is how i solved this issue:

 Private Sub txtPhone_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPhone.Validated
        If IsNumeric(txtPhone.Text) = True Then
        ElseIf txtPhone.Text = "" Then
            'nothing
        Else
            MessageBox.Show("You must enter a numeric value.", "Phone # Data Error")
            txtPhone.Focus()
        End If
    End Sub

Thanks for all your comments
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.