PC Review


Reply
Thread Tools Rate Thread

Check for incorrect characters

 
 
Darren Hill
Guest
Posts: n/a
 
      8th Jan 2008
I have a textbox or two that I want to run some validation on.
Basically, I only want to allow numbers, or uppercase or lowercase
letters, or commas, spaces, apostrophes, and full stops. (, '.)

What code would I need to restrict entry to these possibilities?

Or failing that, after they have been entered, to validate against them
to report an error.

Thanks,

Darren
 
Reply With Quote
 
 
 
 
Zone
Guest
Posts: n/a
 
      8th Jan 2008
Darren, you do not say whether your textbox is on a userform or on a
worksheet. If it's on a userform and you have a command button to unload
the form, you could use something like this. Note that Str should be all on
one line with a space after the z. HTH, James
Private Sub CommandButton1_Click()
Const Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789,'."
Dim j As Integer, chr As String, Flag As Boolean
Flag = False
For j = 1 To Len(TextBox1)
chr = Mid(TextBox1, j, 1)
If Not InStr(Str, chr) > 0 Then Flag = True
Next j
If Flag Then
MsgBox "Only these characters are allowed: " & Str
Else
'do your stuff
Unload Me
End If
End Sub


"Darren Hill" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a textbox or two that I want to run some validation on.
> Basically, I only want to allow numbers, or uppercase or lowercase
> letters, or commas, spaces, apostrophes, and full stops. (, '.)
>
> What code would I need to restrict entry to these possibilities?
>
> Or failing that, after they have been entered, to validate against them to
> report an error.
>
> Thanks,
>
> Darren



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      8th Jan 2008
This'll stop the typing of those characters:

Option Explicit
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


Select Case KeyAscii
'numbers, letters, commas, spaces, apostrophes, and full stops
Case Asc("0") To Asc("9"), Asc("a") To Asc("z"), Asc("A") To Asc("Z"), _
Asc(","), Asc(" "), Asc("'"), Asc(".")
'ok
Case Else
KeyAscii = 0
Beep
End Select

End Sub


Darren Hill wrote:
>
> I have a textbox or two that I want to run some validation on.
> Basically, I only want to allow numbers, or uppercase or lowercase
> letters, or commas, spaces, apostrophes, and full stops. (, '.)
>
> What code would I need to restrict entry to these possibilities?
>
> Or failing that, after they have been entered, to validate against them
> to report an error.
>
> Thanks,
>
> Darren


--

Dave Peterson
 
Reply With Quote
 
Darren Hill
Guest
Posts: n/a
 
      9th Jan 2008
Nice suggestion. I thouyght of using Instr, but was doing it the other
way around: if Instr(textbox.text, "a"), then again for "b", and so on.
I knew there had to be a better way!

Darren
Zone wrote:
> Darren, you do not say whether your textbox is on a userform or on a
> worksheet. If it's on a userform and you have a command button to unload
> the form, you could use something like this. Note that Str should be all on
> one line with a space after the z. HTH, James
> Private Sub CommandButton1_Click()
> Const Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
> 0123456789,'."
> Dim j As Integer, chr As String, Flag As Boolean
> Flag = False
> For j = 1 To Len(TextBox1)
> chr = Mid(TextBox1, j, 1)
> If Not InStr(Str, chr) > 0 Then Flag = True
> Next j
> If Flag Then
> MsgBox "Only these characters are allowed: " & Str
> Else
> 'do your stuff
> Unload Me
> End If
> End Sub
>
>
> "Darren Hill" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I have a textbox or two that I want to run some validation on.
>> Basically, I only want to allow numbers, or uppercase or lowercase
>> letters, or commas, spaces, apostrophes, and full stops. (, '.)
>>
>> What code would I need to restrict entry to these possibilities?
>>
>> Or failing that, after they have been entered, to validate against them to
>> report an error.
>>
>> Thanks,
>>
>> Darren

>
>

 
Reply With Quote
 
Darren Hill
Guest
Posts: n/a
 
      9th Jan 2008
Ah, excellent. So it is possible to stop the entry of illegal
characters.

Thanks, Dave.

Darren

Dave Peterson wrote:
> This'll stop the typing of those characters:
>
> Option Explicit
> Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
>
>
> Select Case KeyAscii
> 'numbers, letters, commas, spaces, apostrophes, and full stops
> Case Asc("0") To Asc("9"), Asc("a") To Asc("z"), Asc("A") To Asc("Z"), _
> Asc(","), Asc(" "), Asc("'"), Asc(".")
> 'ok
> Case Else
> KeyAscii = 0
> Beep
> End Select
>
> End Sub
>
>
> Darren Hill wrote:
>> I have a textbox or two that I want to run some validation on.
>> Basically, I only want to allow numbers, or uppercase or lowercase
>> letters, or commas, spaces, apostrophes, and full stops. (, '.)
>>
>> What code would I need to restrict entry to these possibilities?
>>
>> Or failing that, after they have been entered, to validate against them
>> to report an error.
>>
>> Thanks,
>>
>> Darren

>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Jan 2008
Well, maybe...

You notice that I wrote:
"This'll stop the typing of those characters:"

It doesn't stop the user from pasting anything into that textbox.



Darren Hill wrote:
>
> Ah, excellent. So it is possible to stop the entry of illegal
> characters.
>
> Thanks, Dave.
>
> Darren
>
> Dave Peterson wrote:
> > This'll stop the typing of those characters:
> >
> > Option Explicit
> > Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
> >
> >
> > Select Case KeyAscii
> > 'numbers, letters, commas, spaces, apostrophes, and full stops
> > Case Asc("0") To Asc("9"), Asc("a") To Asc("z"), Asc("A") To Asc("Z"), _
> > Asc(","), Asc(" "), Asc("'"), Asc(".")
> > 'ok
> > Case Else
> > KeyAscii = 0
> > Beep
> > End Select
> >
> > End Sub
> >
> >
> > Darren Hill wrote:
> >> I have a textbox or two that I want to run some validation on.
> >> Basically, I only want to allow numbers, or uppercase or lowercase
> >> letters, or commas, spaces, apostrophes, and full stops. (, '.)
> >>
> >> What code would I need to restrict entry to these possibilities?
> >>
> >> Or failing that, after they have been entered, to validate against them
> >> to report an error.
> >>
> >> Thanks,
> >>
> >> Darren

> >


--

Dave Peterson
 
Reply With Quote
 
Darren Hill
Guest
Posts: n/a
 
      10th Jan 2008
Ooh, I'm glad you pointed that out.
I'll use both validation and this method, then.

Thanks for the warning,

Darren
Dave Peterson wrote:
> Well, maybe...
>
> You notice that I wrote:
> "This'll stop the typing of those characters:"
>
> It doesn't stop the user from pasting anything into that textbox.
>
>
>
> Darren Hill wrote:
>> Ah, excellent. So it is possible to stop the entry of illegal
>> characters.
>>
>> Thanks, Dave.
>>
>> Darren
>>
>> Dave Peterson wrote:
>>> This'll stop the typing of those characters:
>>>
>>> Option Explicit
>>> Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
>>>
>>>
>>> Select Case KeyAscii
>>> 'numbers, letters, commas, spaces, apostrophes, and full stops
>>> Case Asc("0") To Asc("9"), Asc("a") To Asc("z"), Asc("A") To Asc("Z"), _
>>> Asc(","), Asc(" "), Asc("'"), Asc(".")
>>> 'ok
>>> Case Else
>>> KeyAscii = 0
>>> Beep
>>> End Select
>>>
>>> End Sub
>>>
>>>
>>> Darren Hill wrote:
>>>> I have a textbox or two that I want to run some validation on.
>>>> Basically, I only want to allow numbers, or uppercase or lowercase
>>>> letters, or commas, spaces, apostrophes, and full stops. (, '.)
>>>>
>>>> What code would I need to restrict entry to these possibilities?
>>>>
>>>> Or failing that, after they have been entered, to validate against them
>>>> to report an error.
>>>>
>>>> Thanks,
>>>>
>>>> Darren

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
incorrect characters printing envelope =?Utf-8?B?am12YXR0eQ==?= Microsoft Word Document Management 2 7th Oct 2007 02:39 AM
Incorrect Characters being printed to the screen - sometimes =?Utf-8?B?U2ltb24gVw==?= Windows Vista Hardware 1 29th Jun 2007 11:26 PM
Incorrect display of characters Max Microsoft Frontpage 8 16th Jul 2004 04:00 AM
Incorrect characters / codepage Oldman Windows XP General 0 26th Sep 2003 12:07 PM
extended characters incorrect john Microsoft Windows 2000 0 26th Aug 2003 06:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:34 AM.