Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-02
9
Medium Priority
?
121 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
Industry Leaders: 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!

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1000 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 32

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 500 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 22

Assisted Solution

by:Roy Cox
Roy Cox earned 500 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 22

Expert Comment

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

Expert Comment

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

Featured Post

Technology Partners: 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

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Suggested Courses

721 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