Solved

Restricting text box entry from \/:?<>"|

Posted on 2016-09-02
9
110 Views
Last Modified: 2016-09-03
There's gotta be an easier way to code this...and get the " part to work, too:

Private Sub txtDocTitle_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii
    Case Asc("\")
    GoTo NotAllowed:
    Case Asc("/")
    GoTo NotAllowed:
    Case Asc(":")
    GoTo NotAllowed:
    Case Asc("?")
    GoTo NotAllowed:
    Case Asc("<")
    GoTo NotAllowed:
    Case Asc(">")
    GoTo NotAllowed:
    'case asc(""")
    'GoTo NotAllowed:
    Case Else
    
End Select

Exit Sub

NotAllowed:
    strAlerts = "\ / : ? < > " & Chr(34) & " are not allowed in the title."
    Call AlertsCaption_(strAlerts)
    KeyAscii = 0

End Sub

Open in new window

0
Comment
Question by:Roger Hardy
[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
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 41782357
The code is correct.
The only improvement I can see is that you can eliminate the "Case Else" as it's empty.
Also if you want to forbid the " character you should just delete the comments: remove the apostrophe there is at the beginning of the 'case asc("""")   and also remove the apostrophe there is at the beginning of the next line.

So the code become like this:

Private Sub txtDocTitle_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii
    Case Asc("\")
    GoTo NotAllowed:
    Case Asc("/")
    GoTo NotAllowed:
    Case Asc(":")
    GoTo NotAllowed:
    Case Asc("?")
    GoTo NotAllowed:
    Case Asc("<")
    GoTo NotAllowed:
    Case Asc(">")
    GoTo NotAllowed:
    case asc(""")
    GoTo NotAllowed:
   
End Select

Exit Sub

NotAllowed:
    strAlerts = "\ / : ? < > " & Chr(34) & " are not allowed in the title."
    Call AlertsCaption_(strAlerts)
    KeyAscii = 0

End Sub 

Open in new window


Hope it helps. Regards
0
 

Author Comment

by:Roger Hardy
ID: 41782364
The apostrophies are there to comment-out what was erroring out.  I get an error when trying to write the code.  

Compile error:
Expected: list separator or )

Turns the Case Asc(""") line red.

Figured the """ was incorrect.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41782389
You need to double up the double quotes character that is part of the string data:
 
Case Asc("""")  

Open in new window

should work
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 41782397
To avoid using GoTo (a hangover from spaghetti basic) and to shorten the code, you might prefer to use something like this:
Private Sub txtDocTitle_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim strAlerts As String
Dim strIllegals As String

strAlerts = " \ /  :? < > " & Chr(34)
strIllegals = Replace(strAlerts, " ", "")
If InStr(strIllegals, Chr(KeyAscii)) > 0 Then
    strAlerts = strAlerts & " are not allowed in the title."
    Call AlertsCaption_(strAlerts)
    KeyAscii = 0
End If

End Sub

Open in new window

0
 
LVL 31

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 125 total points
ID: 41782553
You may also try it like this......

Private Sub txtDocTitle_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii
    Case Asc("\"), Asc("/"), Asc(":"), Asc("?"), Asc("<"), Asc(">"), Asc("""")
    GoTo NotAllowed:
End Select
Exit Sub

NotAllowed:
    strAlerts = "\ / : ? < > " & Chr(34) & " are not allowed in the title."
    Call AlertsCaption_(strAlerts)
    KeyAscii = 0

End Sub

Open in new window

0
 
LVL 20

Assisted Solution

by:Roy Cox
Roy Cox earned 125 total points
ID: 41782565
I don't see the point in the GoTo
Option Explicit

Private Sub txtDocTitle_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Select Case KeyAscii
    Case Asc("\"), Asc("/"), Asc(":"), Asc("?"), Asc("<"), Asc(">")
        strAlerts = "\ / : ? < > " & Chr(34) & " are not allowed in the title."
        Call AlertsCaption_(strAlerts)
        KeyAscii = 0
        Exit Sub
    End Select

End Sub

Open in new window

0
 

Author Closing Comment

by:Roger Hardy
ID: 41782807
Thanks to all.  Perfect!
0
 
LVL 20

Expert Comment

by:Roy Cox
ID: 41782824
Pleased to help
0
 
LVL 31

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41782845
You're welcome Roger! Glad to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Suggested Courses
Course of the Month7 days, 17 hours left to enroll

617 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