?
Solved

How to search a character in a string?

Posted on 2003-12-04
5
Medium Priority
?
303 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
[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
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 240 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 86

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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