Prevent punctuation in textbox

Posted on 2011-04-21
Last Modified: 2012-08-14
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.

Question by:al4629740
    LVL 39

    Accepted 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.

    Author Comment

    what do you mean handle the paste operation?
    LVL 17

    Expert Comment

    by:Carlos Villegas
    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?
    LVL 14

    Expert Comment

    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

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Introduction While answering a recent question ( 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…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    745 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

    17 Experts available now in Live!

    Get 1:1 Help Now