Solved

check for numeric value function (easy point)

Posted on 2007-03-23
8
819 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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