Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-02
9
Medium Priority
?
127 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
  • 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
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!

 
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 33

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 33

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

963 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