• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

Prevent punctuation in textbox

I have VB6 and I want to prevent the following character from being entered in a text box when they enter their names: '

It is an apostrophe I believe.

1 Solution
try like this

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = Asc("'") Then
        KeyAscii = 0
    End If
End Sub

Open in new window

also you need to handle the paste operation too.

are you trying to avoid the error caused while updating database?
if that is the case there is another way to handle it.
in case you want to handle the db update post your exsting code.
al4629740Author Commented:
what do you mean handle the paste operation?
Carlos VillegasFull Stack .NET DeveloperCommented:
And before use it, you can do this to make sure:
Text1.Text = Replace(Text1.Text, "'", "")

Open in new window

Why are you doing that? preparing for SQL?
Use yv989c's code in your text box's Change event, and that will handle the paste operation. See, not only can user type into the box, but they can have something in the clipboard, place their mouse in your text box, and select "Paste". Or, they can use Ctrl+V, or even Shift+Insert. You have to catch these types of input also.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now