How to search a character in a string?

Hi experts,

in my application, user need to enter a value before proceed. this value is needed to be put in a formula for calculation.. so this value need to be valid.

So if user put something like 1.23.4 (there are two dots), this is not a valid value anymore because i think VB cannot identify this value in calculation (mathematical).

How i want to code so that if user keyin more then one dot ("."), my application will pop up a msgbox indicate user to change the value. I already code for not allowed user to key in alphabet characters. so that is not a problem.. the problem only this dot ("."). i kindda think of to search the character "." , if it is more than one, so error msg will popup. But i don't know how to code it :)

thanks
Diode_TempAsked:
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.

bobbit31Commented:
if it's a number you are looking for:

if IsNumeric(<value>) then
  '' it's a valid number
else
  '' it's not
end if
0
prasitleeCommented:
Dear Diode_Temp,
    Try the following code. I am strongly sure that it would give you the idea to detect the number of dot which is greater than one.

Option Explicit

Private m_bDotExists As Boolean

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If InStr(Text1.Text, ".") = 0 Then
        m_bDotExists = False
    Else
        m_bDotExists = True
    End If
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    If m_bDotExists Then
        If Mid(Text1.Text, Len(Text1.Text) - 1, 1) = "." Then
            MsgBox "More than one dot."
        End If
    End If
End Sub

    I am strongly sure that my comment has already answered your question.
                                                                                   Meng
0
Tommy KinardCommented:
Hi Diode_Temp,

The below sub will display a message when the second decimal is entered.

Private Sub Text1_Change()
    If InStr(InStr(1, Text1.Text, ".") + 1, Text1.Text, ".") > 0 Then
        MsgBox "Invalid Data Entered!"
    End If
End Sub

HTH
dragontooth

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
Mike TomlinsonMiddle School Assistant TeacherCommented:
If your interested...this is my version of how to make a textbox that allows only a decimal value.  Negative values are allowed.  At any given time, there is always a valid number in the box.

Idle_Mind

Private Sub Form_Load()
    Text1.Text = "0"
    Text1.SelStart = 0
    Text1.SelLength = 1
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim insertPoint As Integer
    Dim char As String
   
    insertPoint = Text1.SelStart
    char = Chr(KeyAscii)
   
    If (KeyAscii >= 48 And KeyAscii <= 57) Then ' Digits 0 thru 9
        If KeyAscii = 48 Then
            If Text1.Text = "0" And Text1.SelStart = 0 And Text1.SelLength = 1 Then
                Text1.SelStart = 1
                Text1.SelLength = 0
                KeyAscii = 0
            ElseIf Text1.Text = "-0" And Text1.SelStart = 1 And Text1.SelLength = 1 Then
                Text1.SelStart = 2
                Text1.SelLength = 0
                KeyAscii = 0
            ElseIf Text1.Text = "-.0" And Text1.SelStart = 2 And Text1.SelLength = 1 Then
                Text1.SelStart = 3
                Text1.SelLength = 0
                KeyAscii = 0
            End If
        End If
        Exit Sub
    ElseIf KeyAscii = 8 Then ' BackSpace
        If Text1.Text = ".0" Then
            If (Text1.SelStart = 1 And Text1.SelLength = 1) Or Text1.SelStart = 2 Then
                Text1.Text = "0"
                Text1.SelStart = 0
                Text1.SelLength = 1
                KeyAscii = 0
            End If
        ElseIf Text1.Text = "-0" Then
            If Text1.SelStart = 1 And Text1.SelLength = 1 Then
                Text1.Text = "0"
                Text1.SelStart = 0
                Text1.SelLength = 1
                KeyAscii = 0
            End If
        ElseIf Text1.Text = "-.0" Then
            If Text1.SelStart = 2 And Text1.SelLength = 1 Then
                Text1.Text = "-0"
                Text1.SelStart = 1
                Text1.SelLength = 1
                KeyAscii = 0
            End If
        End If
        Exit Sub
    ElseIf char = "." Then
        If InStr(Text1.Text, ".") > 0 Then
            KeyAscii = 0
        End If
        Exit Sub
    ElseIf char = "-" Then ' Toggle Minus Sign
        If Left(Text1.Text, 1) = "-" Then
            Text1.Text = Mid(Text1.Text, 2)
            insertPoint = insertPoint - 1
        Else
            Text1.Text = "-" & Text1.Text
            insertPoint = insertPoint + 1
        End If
        Text1.SelStart = insertPoint
        KeyAscii = 0
        Text1_Change
        Exit Sub
    End If
    KeyAscii = 0 ' Cancel Everything Else
End Sub

Private Sub Text1_Change() ' Make sure we have a number in the box at all times
    Dim validChars As String
    Dim a As Integer
   
    validChars = "-.0123456789"
   
    If Text1.Text = "" Or Text1.Text = "0" Then
        Text1.Text = "0"
        Text1.SelStart = 0
        Text1.SelLength = 1
        Exit Sub
    ElseIf Text1.Text = "." Or Text1.Text = ".0" Then
        Text1.Text = ".0"
        Text1.SelStart = 1
        Text1.SelLength = 1
        Exit Sub
    ElseIf Text1.Text = "-." Or Text1.Text = "-.0" Then
        Text1.Text = "-.0"
        Text1.SelStart = 2
        Text1.SelLength = 1
        Exit Sub
    ElseIf Text1.Text = "-" Or Text1.Text = "-0" Then
        Text1.Text = "-0"
        Text1.SelStart = 1
        Text1.SelLength = 1
        Exit Sub
    End If
   
    ' code for handling when text is pasted into the box
    For a = 1 To Len(Text1.Text)
        If InStr(validChars, Mid(Text1.Text, a, 1)) = 0 Then
            Text1.Text = "0"
            Text1.SelStart = 0
            Text1.SelLength = 1
            Exit Sub
        End If
    Next a
   
    a = InStr(Text1.Text, "-")
    If a > 0 Then
        If InStr(a + 1, Text1.Text, "-") > 0 Then
            Text1.Text = "0"
            Text1.SelStart = 0
            Text1.SelLength = 1
            Exit Sub
        End If
    End If
   
    a = InStr(Text1.Text, ".")
    If a > 0 Then
        If InStr(a + 1, Text1.Text, ".") > 0 Then
            Text1.Text = "0"
            Text1.SelStart = 0
            Text1.SelLength = 1
            Exit Sub
        End If
    End If
End Sub
0
Tommy KinardCommented:
Glad I could help!
dragontooth

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 Classic

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.