Solved

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

Posted on 2016-09-02
9
86 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 29

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 18

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 18

Expert Comment

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

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

810 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