Solved

check for numeric value function (easy point)

Posted on 2007-03-23
8
815 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:jsctechy
8 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18783898
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18783912
"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
 
LVL 1

Author Comment

by:jsctechy
ID: 18783927
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
 
LVL 1

Author Comment

by:jsctechy
ID: 18783952
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:jsctechy
ID: 18783966
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
 
LVL 8

Accepted Solution

by:
Autoeforms earned 500 total points
ID: 18784817
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18784899
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
 
LVL 1

Author Comment

by:jsctechy
ID: 18804108
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now