Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit input to unbound form field

Status
Not open for further replies.

helpneeded

Programmer
Aug 24, 1999
15
GB
I have an unbound form and want to limit the number of characsters entered in each field, there must be a simple way of doing this, but I'm feeling really thick.

Thanks in advance

Chris
 
In the text box control's "Input Mask" property put

CCCCCCCCCCCCCCC

As many Cs as you want to be the maximum number of allowable chars.

In addition to 'C' the Input Mask recognises:-
Code:
Character Description 
0         Digit (0 to 9, entry required, plus [+] and minus [-] signs not allowed). 
9         Digit or space (entry not required, plus and minus signs not allowed). 
#         Digit or space (entry not required; spaces are displayed as blanks while in Edit mode, but blanks are removed when data is saved; plus and minus signs allowed). 
L         Letter (A to Z, entry required). 
?         Letter (A to Z, entry optional). 
A         Letter or digit (entry required). 
a         Letter or digit (entry optional). 
&         Any character or a space (entry required). 
C         Any character or a space (entry optional). 
. , : ; - / Decimal placeholder and thousand, date, and time separators. (The actual character used depends on the settings in the Regional Settings Properties dialog box in Windows Control Panel). 
<         Causes all characters to be converted to lowercase. 
>         Causes all characters to be converted to uppercase. 
!         Causes the input mask to display from right to left, rather than from left to right. Characters typed into the mask always fill it from left to right. You can include the exclamation point anywhere in the input mask. 
\         Causes the character that follows to be displayed as the literal character (for example, \A is displayed as just A).
 
yes thats the short and easy way to do it.
another way is to use some code to test the length of the data entered using the AfterUpdate Event, and display a message box if the length is too long.

Private Sub Text11_AfterUpdate()
If Len(Text11) > 10 Then
If MsgBox(&quot;You are Limited to 10 Characters for this Field. Your entry of &quot; _
& vbCrLf & Chr(34) & Text11 & Chr(34) & &quot; is &quot; & Len(Text11) & &quot; Characters Long &quot; & vbCrLf & _
&quot;Do you wish to use &quot; & vbCrLf & Chr(34) & Left(Text11.value, 10) & Chr(34) & vbCrLf & &quot;as your entry?&quot;, vbQuestion + vbYesNo, &quot;Entry to Long&quot;) = vbYes Then
Text11 = Left(Text11.value, 10)
Else
Text11 = &quot;&quot;
End If
End If
End Sub

PaulF
 
Thanks for that LittleSmudge, Only problem is that the fields vary from 8 - 255 characters. I don't really want to enter 255 C's. There must be a way of programatically limiting input. I have even messed with KeyPress event to check number of key presses and used a case statement to check which key is pressed, very messy. I've also tried Len(strNote)>255 after update.

Thanks for your help
 
I don't really want to enter 255 C's

Why not ?

In the spirit of KISS ( Keep it Simple .. .. ) then go for the easy option every time - and the propery box will take it.

To make life easier put in 25 Cs then copy to clipboard and then paste 9 more lots, then add five more Cs - You don't literally have to count 255 Cs.


G LS
 
Point taken LittleSmudge, but I don't like simple solutions when a more complicated one can be found. I like to use a hammer when a nut cracker would do.

Cheers

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top