Solved

How to search a character in a string?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now