Solved

check for numeric value function (easy point)

Posted on 2007-03-23
8
818 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - Get ColumnNames of SQL Table 4 15
vb.net class 3 22
Help with opening combobox using VB.NET. 3 26
How to seperate number and letters from a string 3 22
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

827 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