![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Apr 2002
Posts: 27
|
I'm checking and counting characters as they're entered in a textbox named CLLI with "mystr = Right(CLLI, 1)" and a counter, but I can't get a check to work for an "Enter". I want to make sure exactly 8 alphabetic and/or numeric characters are entered. Is there an easier way to do that?
Thanks, Bidwin |
|
|
|
|
|
#2 |
|
New Member
Join Date: Apr 2002
Posts: 41
|
Bidwin
I'm not entirely sure what you mean, but the Enter event is triggered when the user first enters the textbox. To test the input, I suggest using the BeforeUpdate event as you can stop update if the input is outside your requirements. Try pasting this code in the code pane for the userform: Private Sub CLL1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean) With CLL1 If Len(.Text) <> 8 Then Cancel = True MsgBox "You nust enter 8 characters" Else End If End With End Sub Amend the name if the textbox is not named CLL1. What this will do is: - Check the input AFET the user has finished and BEFORE any update of records - if the length of the input is not 8, a messgae box will be presented and the update will not occur - user will remain in the textbox Any help? Regards Robb__ |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
I would change the
Len(TextBox1) <> 8 with Len(TextBox1) > 8 Why ? because, if you're just starting to type, a MessageBox will appear ALWAYS until you have FINALLY (After 8 annoying messages) reached the 8. Also, this could be helpful: If Len(TextBox1) > 8 then TextBox1 = Left(TextBox1,8) End If to trim the TextBox1 to 8 characters if the user enters more. |
|
|
|
|
|
#4 |
|
New Member
Join Date: Apr 2002
Posts: 41
|
I agree with Juan if you are using the Change event for the test. I was, however, suggesting the BeforeUpdate event and, I think, using that it would be sufficient to test <> 8. Up to you.
Hope it all helps. Regards Robb__ |
|
|
|
|
|
#5 |
|
New Member
Join Date: Apr 2002
Posts: 27
|
Robb and Juan Pablo,
Thanks for your replies. By "Enter" I meant the Enter key, not the enter function, if that's the right word. I'm still trying to teach myself VB and userforms, using VERY old programming techniques, ones that required the programmer to do everything. VB seems to have many routines and functions that are triggered by conditions, and I'm not yet familiar with those conditions. So, if you see postings from me they probably will asking for help and not giving it. And at times I may do things the hard way just to see how they work, like this one. Thanks again for your replies. Bidwin PS: Why would displaying a msgbox stop code processing? That's happened to me a few times, and the only remedy I've found is to eliminate the msgbox display. |
|
|
|
|
|
#6 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
Properties & Methods for help on these controls eg Textbox has a MaxLength property you can set this to 8...... |
|
|
|
|
|
|
#7 |
|
Board Regular
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
|
Hi Bidwin
A bit more info... To make sure the user has entered alpha-numeric data, search help for "Option Compare". You can use this to test for what data was entered... Also check into the "KeyPress, KeyUp, KeyDown" events for good validation techniques as well. Tom |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|