Valid. characters - What's Wrong With My Code?

G

Guest

Private Sub txtCallerName_KeyPress(KeyAscii As Integer)
On Error GoTo Err_txtCallerName_KeyPress
Select Case KeyAscii
'65-90 are caps 97-122 are lower case; tab (9) and backspace (8) okay
Case 65 To 90, 97 To 122, 8, 9
Case Else
'Setting KeyAscii to zero cancels the key input
KeyAscii = 0
MsgBox "Please enter alphabetical characters only!"
End Select
Exit_txtCallerName_KeyPress
Exit Sub
Err_txtCallerName_KeyPress
MsgBox Err.Description
Resume Exit_txtCallerName_KeyPress
End Sub

Can someone tell me why this doesn't work?
 
S

Sandra Daigle

Hi Sandy,

What isn't working? It's always a good idea to be more specific so we aren't
chasing shadows :)

Do you have the KeyPreview property of the form set to True? This is on the
Event tab of the form's property sheet. Also, I suspect you are getting
compile errors because there are no colons after your labels for the Error
handlers:

Exit_txtCallerName_KeyPress:

Err_txtCallerName_KeyPress:
 
G

Guest

looks like your error trap is wrong

Private Sub txtCallerName_KeyPress(KeyAscii As Integer)
On Error GoTo Err_txtCallerName_KeyPress '<--- this is the "goto" name if
there is an error

Select Case KeyAscii
'65-90 are caps 97-122 are lower case; tab (9) and backspace (8) okay
Case 65 To 90, 97 To 122, 8, 9
Case Else
'Setting KeyAscii to zero cancels the key input
KeyAscii = 0
MsgBox "Please enter alphabetical characters only!"
End Select
' Exit_txtCallerName_KeyPress '<--- not sure why this is here..if its a
"goto" name you dont have a "goto" call...i would delete it
Exit Sub '<--- this works as the end of your code...and the only way
to run anything after is if there is an error that will jump it to the next
line which is your goto name
Err_txtCallerName_KeyPress: '<--- you need ":" to tell VB its a "goto"
to name
MsgBox Err.Description
Resume Exit_txtCallerName_KeyPress
End Sub
 
G

Guest

oops missed one

Resume Exit_txtCallerName_KeyPress

should be
Resume '<-- go back to line that gave error
or
Resume Next '<-- go back to line after error
or delete line if you just want to exit function if there is an error
 
D

Dirk Goldgar

visdev1 said:
oops missed one

Resume Exit_txtCallerName_KeyPress

should be
Resume '<-- go back to line that gave error
or
Resume Next '<-- go back to line after error
or delete line if you just want to exit function if there is an error

There's nothing wrong with

Resume <some line label>

Except for leaving off the colon on what should be line labels,
"Exit_txtCallerName_KeyPress" and "Err_txtCallerName_KeyPress", I don't
see anything wrong with the error-handling code as Sandy originally had
it. Although it's not necessary in this case, it's a good habit to
Resume at an "Exit" section, in case there's cleanup code that has to be
executed before exiting the procedure.
 
G

Guest

Many thanks to all of you!

Ya' know, I could have stared at this for the next few weeks and not seen
the problem. I wasn't even looking at the error code. It's always good to
have fresh eyes view code . . .

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top