Solved

How to search a character in a string?

Posted on 2003-12-04
5
297 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 115
Sending a email via excel using vba 6 97
Adding to a VBA? 6 77
Child Form in front 4 49
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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