Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7666
  • Last Modified:

vba code to check for special characters in a string

I want a message box to indicate if special charaters are present in the first 7 characters of a text box named Part.  Special characters mean anything other than 0 through 9 and a(A) through z(Z).  ~`!@#$%^&*()-_+=/\|{}[];.,: and space are all examples of special characters to catch.  Could someone please help me with the VBA code to accomplish this.

Left(Me.Part, 7)
0
sxxgupta
Asked:
sxxgupta
  • 3
  • 2
1 Solution
 
Rey Obrero (Capricorn1)Commented:

dim j as integer

for j=1 to 7
   if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",mid(me.part,1,j))=0 then
      msgbox "special character exists"
   end if

next
0
 
thenelsonCommented:
The following code is adapted from  www.thenelson.name/#ParseNames, "    RemoveSpecialCharacers " which has other useful string functions.


Public Function HasSpecialCharacers(ByVal InputString As String) As Boolean
Dim j As Integer, tmpString  As String * 1

For j = 1 To Len(InputString)
    tmpString = Mid(InputString, j, 1)
    If (tmpString < "a" Or tmpString > "Z") And (tmpString < "0" And tmpString > "9") Or tmpString = " " Then
        HasSpecialCharacers = True
    End If
Next j
End Function
0
 
Rey Obrero (Capricorn1)Commented:
correction on the mid function

dim j as integer

for j=1 to 7
   if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",mid(me.part,j,1))=0 then
      msgbox "special character exists"
   end if

next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sxxguptaAuthor Commented:
Hi capricorn
In your code could you also display the special characters in the msg box please.  Or should I ask for this in a separate question. Thank you.
Sanjay
0
 
Rey Obrero (Capricorn1)Commented:
dim j as integer

for j=1 to 7
   if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",mid(me.part,j,1))=0 then
      msgbox "special character " & mid(me.part,j,1) & " exists"
   end if

next
0
 
sxxguptaAuthor Commented:
Thanks Capricorn.  Merry x-mas.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now