ensuring ONLY letters are allowed to be entered into a text box

to ensure that text entered into a text box was letters only i have been given this:

If IsNumeric(txtTitle.Text) = True Then
'Code to go here when the data entered in textbox is numeric
MsgBox "Please enter a title !"
txtTitle.Text = ""
txtTitle.SetFocus
Else
'Code to go here when it is NOT numerical ie an error message box, you can leave this part blank because obviously
'it is letters and so you do not need to do anything

however this code only works if the user enters all numbers as a job title such as 333etc

if i enter head barma2n then the code allows this. I want ONLY letters to be allowed.

any1 help?

End If
scm0smlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
Hi scm0sml,

Use the KeyPress event:

Private Sub txtTitle_KeyPress(KeyAscii As Integer)
If Instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",UCase(Chr(KeyAscii))) > 0 Then
  'This is OK
Else
  KeyAscii = 0
  'It isn't a letter so set KeyAscii to 0 to ignore that keypress
End If
End Sub

This will determine whether you have pressed a letter key or not, if not then that keypress is voided. This prevents you from entering in any character not in the list at source.

Tim Cottee
0
gpaulishCommented:
Being that you asked to ignore only numerics and not other character symbols this logic may make more sense.  It will allow all characters except numerics, without ignoring potentially important characters like spaces.  

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
    End If
End Sub

-GP
0
scm0smlAuthor Commented:
ive inserted all of these procedures into my program, one at a time obviously and none of them r having any effect?

am i doing sumthing wrong?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

scm0smlAuthor Commented:
hello? any1?

neither procedure is stopping invalid input
0
Shane Russell2nd Line Desktop SupportCommented:
You could go through the string using a for next loop and check each value against the IsNumeric function and if it is to not allow that string or at least not that numeric value, do this on the lost focus.

I will check this in VBA so I can figure something out for you but that is what I think you are after :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shane Russell2nd Line Desktop SupportCommented:
Your best bet thinking about it would to be either to make your own text control to not allow numerical values to be entered what so ever or make your own function to check what has been entered and get rid of numerical values
0
Shane Russell2nd Line Desktop SupportCommented:
I found this , this might give you some idea of how to do it with regards to functions :

http://www.taltech.com/resources/intro_to_bc/UPC.vb
0
Shane Russell2nd Line Desktop SupportCommented:
There is the masked edit control you can format it to only allow text, thinking of that there is also something simmiliar for text fields to only allow data entry of textual type:

http://www.vbexplorer.com/VBExplorer/viewcode.asp?SendText=vb_faq/faq_090598_ht
0
Shane Russell2nd Line Desktop SupportCommented:
You could also set the max length of the textbox to a certain amount
0
gpaulishCommented:
There is not reason to offer any other solutions than the one I posted, it certainly does work correct.

Keep this in mind, this sub is depended upon your control name, for example my textbox was called
Text1, hence the Sub Text1_KeyPress, if you named your textbox anything other than Text1, then of
course this will not work, so change the name of the sub, or textbox to match.

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
    End If
End Sub



-GP
0
Shane Russell2nd Line Desktop SupportCommented:
http://www.codeproject.com/useritems/Complete_Textbox.asp

Also I think I found the names of the propertys :

DisplayFormat or CustomFormat, those ones allow you to format a textbox and that should only allow data entry of text if you set it to text if that does not work you can also use the Format Function along with it

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctFormat.asp

Where you can Create your own user-defined string formats.

Other then that, ~GP's suggestion was good :)
0
scm0smlAuthor Commented:
sorry lads but......

ive got exactly this:
Private Sub txtTitle_KeyPress(ByVal KeyAscii As Integer)
        If (KeyAscii >= 48 And KeyAscii <= 57) Then
            KeyAscii = 0
        End If
    End Sub

with my text box obviously being called txtTitle and nothing happens when i type into the box, as in any entry is allowed.

any thoughts?

sorry to be a pain but this looks by far the simplest way to do it if it would work!!
0
scm0smlAuthor Commented:
the problem is with the KeyPress method. i have tried changing the text of a label and its not working.
0
Shane Russell2nd Line Desktop SupportCommented:
What about using the suggestions I made ?
0
Shane Russell2nd Line Desktop SupportCommented:
scm0sml, HERE IS THE PERFECT EXAMPLE FOR YOU :

http://www.thescarms.com/vbasic/SubClassTextBox.asp

go download that, you will have to alter the code so it lets you enter regular text as it converts it to upper case but that shouldnt be  a problem :)
0
Shane Russell2nd Line Desktop SupportCommented:
0
Shane Russell2nd Line Desktop SupportCommented:
Instead of using the keyascii codes 48 and 57, what are the constants ?
0
gpaulishCommented:
are you using visual basic 6.0?
0
Shane Russell2nd Line Desktop SupportCommented:
yes he is :)
0
gpaulishCommented:
Then for it not to work makes little sense.  Have you placed a break point in the sub to see if that event was being fired?????
0
scm0smlAuthor Commented:
im using vb.net lads, think thatmay have been where our prob is??
0
scm0smlAuthor Commented:
finally got it working with this but ive split the points between th 2 of u as u both put a fair bit into it!!
cheers!

Private Sub TitleValidity()
        Dim validString As String = "ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz ,;.'"
        Dim i, j As Integer
        Dim charvalid As Boolean

        For i = 0 To txtTitle.Text.Length - 1
            For j = 0 To validString.Length - 1
                If txtTitle.Text.Chars(i) = validString.Chars(j) Then
                    charvalid = True
                End If
            Next
            If j = validString.Length And charvalid = False Then
                MsgBox("invalid title")
            End If
        Next
    End Sub
0
Shane Russell2nd Line Desktop SupportCommented:
Thank you for the points and grade, glad you got your problem solved !
0
gpaulishCommented:
ohh yes vb.net would be a problem as events are handled differently, thanks for all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.