Character Prevention

How do I prevent a user from using , (comma) or " (quotes) in a text box.
rharizAsked:
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.

rharizAuthor Commented:
null
0
rharizAuthor Commented:
Edited text of question.
0
ggilmanCommented:
You can capture the text change event for the text box.


Private Sub Text1_Change()
    Dim myText As String
    Dim testText As String
    Dim char As String
    Dim i As Long
   
    If InStr(Text1.Text, ",") Or InStr(Text1.Text, """") Then
        testText = Text1.Text
        For i = 1 To Len(testText)
            char = Mid(testText, i, 1)
            If char <> "," And char <> """" Then
                myText = myText & char
            End If
        Next i
        Text1.Text = myText
    End If
End Sub
0
MSSPs - Are you paying too much?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

ggilmanCommented:
Same as other post but now commented

Option Explicit
Private Sub Text1_Change()
    Dim myText As String
    Dim testText As String
    Dim char As String
    Dim i As Long
   
    'Test to see if any invalid charachters are being used
    If InStr(Text1.Text, ",") Or InStr(Text1.Text, """") Then
        testText = Text1.Text
       
        'Loop though all of the charachters in the string
        For i = 1 To Len(testText)
       
            'Grab one charachter at a time
            char = Mid(testText, i, 1)
           
            'If it isn't an offending charchter, keep it
            If char <> "," And char <> """" Then
                myText = myText & char
            End If
        Next i
       
        'Replace the value in the text box with our value
        Text1.Text = myText
    End If
End Sub
0
ggilmanCommented:
This code is a little smaller and you won't loose your cursor position.

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    Dim position As Long
    If KeyCode = 188 Or KeyCode = 222 Then
        position = Text1.SelStart
        Text1.Text = Mid(Text1.Text, 1, Text1.SelStart - 1) & Mid(Text1.Text, Text1.SelStart + 1)
        Text1.SelStart = position - 1
    End If
End Sub
0
khurramkcsCommented:
Here is what'll do it efficiently:

Private Sub Text1_KeyPress(KeyAscii As Integer)
  ' ASCII code of comma is 44, that of quotes is 34.
  ' But it won't be a good idea to hardcode the codes!
  If KeyAscii = Asc("""") Or KeyAscii = Asc(",") Then
    KeyAscii = 0
  End If
End Sub
0
ggilmanCommented:
Only problem with khurramks code, as well as my second attempt, is that if someone does a copy and then pastes to the text box, a comma or quote can be left. If you can find a way to turn paste off it won't be a problem but otherwise, you might need to go back to my first attempt. Here I added the code so you won't loose your cursor position.

Option Explicit
Private Sub Text1_Change()
    Dim myText As String
    Dim testText As String
    Dim char As String
    Dim i As Long
    Dim position As Long
     
    'Test to see if any invalid charachters are being used
    If InStr(Text1.Text, ",") Or InStr(Text1.Text, """") Then
        testText = Text1.Text
        position = Text1.SelStart
         
        'Loop though all of the charachters in the string
        For i = 1 To Len(testText)
         
            'Grab one charachter at a time
            char = Mid(testText, i, 1)
             
            'If it isn't an offending charchter, keep it
            If char <> "," And char <> """" Then
                myText = myText & char
            End If
        Next i
         
        'Replace the value in the text box with our value
        Text1.Text = myText
        Text1.SelStart = position
    End If
End Sub
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
rharizAuthor Commented:
Can't allow the characters to be pasted.
0
rharizAuthor Commented:
Works like a charm.  This is great that you caught the cursor position moving.  I noticed that in memo fields the cursor moved to the beginning of the line when attempting to insert quotation marks.
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
Hardware

From novice to tech pro — start learning today.