Solved

How to search a character in a string?

Posted on 2003-12-04
5
298 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:Diode_Temp
5 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9878865
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
 
LVL 6

Expert Comment

by:prasitlee
ID: 9878993
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
 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 80 total points
ID: 9879032
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9880383
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
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 9882088
Glad I could help!
dragontooth

0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

685 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