Solved

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

Posted on 2016-09-02
9
62 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 28

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 17

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 17

Expert Comment

by:Roy_Cox
ID: 41782824
Pleased to help
0
 
LVL 28

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.

744 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

16 Experts available now in Live!

Get 1:1 Help Now