Hi all.
Does a textbox on an Access form have an equivalent of the VB6 .MaxLength property? I need to limit the length of an input string. It's not bound to anything, just used in code, but I'd prefer to limit it in the control rather than validating afterward.
27 59058 NeoPa 32,556
Expert Mod 16PB
Specifically no.
But you might want to check out the 'InputMask Property'. It may be able to give the required functionality.
Specifically no.
But you might want to check out the 'InputMask Property'. It may be able to give the required functionality.
Nuts! I was hoping to avoid that.
Thanks, anyway.
Hi all.
Does a textbox on an Access form have an equivalent of the VB6 .MaxLength property? I need to limit the length of an input string. It's not bound to anything, just used in code, but I'd prefer to limit it in the control rather than validating afterward.
In code you could - Dim someStr As String
-
-
someStr = InputBox("Enter some string: ")
-
If Len(someStr) > 100 Then
-
someStr = Left(someStr,100)
-
'Msg to user
-
End If
-
In code you could - Dim someStr As String
-
-
someStr = InputBox("Enter some string: ")
-
If Len(someStr) > 100 Then
-
someStr = Left(someStr,100)
-
'Msg to user
-
End If
-
Or if textbox, damn I should read these things better ... - Dim someStr As String
-
-
someStr = Me.textbox
-
If Len(someStr) > 100 Then
-
someStr = Left(someStr,100)
-
Me.textbox = someStr
-
'Msg to user
-
End If
-
Or if textbox, damn I should read these things better ... - Dim someStr As String
-
-
someStr = Me.textbox
-
If Len(someStr) > 100 Then
-
someStr = Left(someStr,100)
-
Me.textbox = someStr
-
'Msg to user
-
End If
-
I'm now wondering if you could get the realtime text value of the input and validate on KeyPress event. Preformance ???
I'm now wondering if you could get the realtime text value of the input and validate on KeyPress event. Preformance ???
It's ok, I can handle the validation alright. I just didn't want to have to, because .MaxLength is ever so much simpler.
Maybe I need to use the VB6 library ;)
It's ok, I can handle the validation alright. I just didn't want to have to, because .MaxLength is ever so much simpler.
Maybe I need to use the VB6 library ;)
Worth a try as it should be on the list. Never tried it for something like this though.
Mary
Worth a try as it should be on the list. Never tried it for something like this though.
Hm... nah!
I've just spent a few minutes browsing the References list and don't really like what I saw. Think I'll just bite the bullet and put in some code to do validation (or real-time feedback) on the form.
Hm... nah!
I've just spent a few minutes browsing the References list and don't really like what I saw. Think I'll just bite the bullet and put in some code to do validation (or real-time feedback) on the form.
Yeah!
It is a bit of a minefield.
When you get some time :)
Have a look on msdn regarding 'Automation' from Access.
Mary
Yeah!
It is a bit of a minefield.
When you get some time :)
Have a look on msdn regarding 'Automation' from Access.
Will do.
In the meantime, I ended up going with something very simple. I just turn the field background red as long as the value is not within the required length. Although it doesn't account for the possibility of a black (zero) background, here's the routine I used. - Private Sub tx_Change()
-
Static HeldColour As Long
-
With tx
-
If Len(.Text) > 8 Then
-
If HeldColour = 0 Then
-
Beep
-
HeldColour = .BackColor
-
.BackColor = vbRed
-
End If
-
Else
-
If HeldColour <> 0 Then
-
.BackColor = HeldColour
-
HeldColour = 0
-
End If
-
End If
-
End With
-
End Sub
-
If you wanted even more control: - if Len(tx.Text) > 8 then
-
Me.someothercontrol.SetFocus
-
Me.tx = Left(Me.tx, 8)
-
Msgbox("You are limited to 8 characters ....", vbOKOnly + vbWarning)
-
Me.tx.SetFocus
-
Else
-
>
-
>
-
>
-
If you wanted even more control: ...
Yeah, I thought about chopping it off, but decided against it. The users know the limits, if they choose to ignore the warning then so be it.
One technique I've used in similar cases at times is to have a timer on the form. To highlight a field I do one of two things: - set a value in the .Tag property (VB6, not sure about Access) and the timer watches for these flags and flashes the controls on and off.
- Highlight the control (typically by setting background colour), and also flag it in .Tag or whatever. The timer gradually fades the colour back to normal. This one looks quite good, actually. You get a beep, the field turns (typically) bright red, then over a few seconds it fades back to normal, but it doesn't interfere with your typing or anything.
(Note, in cases such as these the warning message might be displayed in a particular place on the form, rather than as a message box).
Damnit, I've got to stop spending so much time here - I keep getting carried away. :(
Never thought about using the control tags in this way. Sounds interesting.
Mary
Never thought about using the control tags in this way. Sounds interesting.
I find them quite useful for this kind of thing.
One caveat, though.
I recommend that one always assume there may be other info in there. I try to - Add my flag onto the end and make it distinctive, with something like "/FLASHFIELD", and
- Remove it without disturbing any other contents.
That way, you can make use of multiple routines that use the .Tag property, without any cross-interference.
A simple hack would be to add a "dummy" field to your table, set its length to 6 in the Table Design View, and bind the text box to it. What is it you don't like about Input Masks?
A simple hack would be to add a "dummy" field to your table, set its length to 6 in the Table Design View, and bind the text box to it. What is it you don't like about Input Masks?
Oh nothing much, I suppose. I think it's just the nuisance of losing the .MaxLength. After all, I don't have the slightest interest in what goes in there, just that it isn't too long. So, it would be fair to say I'm merely being stubborn.
As for the dummy field, it's an interesting idea. Would it limit input, or just spit the dummy if you enter too much?
Might be more trouble than it's worth though - nothing on the form is currently bound to anything. I'm just using the form to get input a la VB6, then building and executing a query, and closing the form.
Setting the length of a text field in Design View for the table limits the input to that many characters. If the user keeps typing the cursor simply doesn't move.
Hi all.
Does a textbox on an Access form have an equivalent of the VB6 .MaxLength property? I need to limit the length of an input string. It's not bound to anything, just used in code, but I'd prefer to limit it in the control rather than validating afterward.
There is a little known and undocumented method for limiting the number of
Input characters in an Access Text Box using the API, here it goes:
Private Declare Function SendMessageLong Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lngValue As Long) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Const EM_SETLIMITTEXT As Long = &HC5 - Private Sub LimitChars(txt As Textbox, lngLimit As Long)
-
Dim hWnd As Long
-
Dim lngResult As Long
-
Dim lngNewMax As Long
-
-
'Get Window Handle for the current Window
-
hWnd = GetFocus()
-
-
'Check for existing length of Text and adjust lngLimit
-
lngNewMax = Len(txt.Text)
-
If lngNewMax < lngLimit Then
-
lngNewMax = lngLimit
-
End If
-
-
'Send the Message to the Text Box to limit itself to lngNewMax _
-
'characters
-
SendMessageLong hWnd, EM_SETLIMITTEXT, lngNewMax, 0
-
End Sub
NOTE: As always, when using the API, the syntax must be exact.
Hook the code in the Change() Event of the Text Box
Nice one, that's going in my keeper box.
Thanks ADezii There is a little known and undocumented method for limiting the number of Input characters in an Access Text Box using the API, here it goes:
Private Declare Function SendMessageLong Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lngValue As Long) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Const EM_SETLIMITTEXT As Long = &HC5 - Private Sub LimitChars(txt As Textbox, lngLimit As Long)
-
Dim hWnd As Long
-
Dim lngResult As Long
-
Dim lngNewMax As Long
-
-
'Get Window Handle for the current Window
-
hWnd = GetFocus()
-
-
'Check for existing length of Text and adjust lngLimit
-
lngNewMax = Len(txt.Text)
-
If lngNewMax < lngLimit Then
-
lngNewMax = lngLimit
-
End If
-
-
'Send the Message to the Text Box to limit itself to lngNewMax _
-
'characters
-
SendMessageLong hWnd, EM_SETLIMITTEXT, lngNewMax, 0
-
End Sub
NOTE: As always, when using the API, the syntax must be exact. Hook the code in the Change() Event of the Text Box
Nice one, that's going in my keeper box.
Thanks ADezii
Ditto. In fact I vaguely recall seeing this years ago when discussing VB, but of course with the MaxLength property it wasn't needed.
Um... one question, though. You say to place this in the Change event? That doesn't sound right. Wouldn't the SendMessage be called once only, at form load or something? (Alright, two questions then). I thought the purpose of it was simply to set the maximum length for future editing, not to check a change which was just made (or attempted).
This is an old thread, but if it helps anybody, there is a much easier way to do this. Simply: - Private Sub ProgramName_Change()
-
If Len(Forms.RequestNew.ProgramName.Text) > 10 Then
-
Forms.RequestNew.ProgramName.Text = Left(Forms.RequestNew.ProgramName.Text, 10)
-
Forms.RequestNew.ProgramName.SelStart = 10
-
End If
-
End Sub
All you have to do is chop the Len of the text when it is over the maxlength and reset the cursor location to the end. Try it.
You may write a simple control into "keypress" event like below example where 24 is the max length. If the user tries to enter more chrs it sends a backspace - Private Sub tPackTX_KeyPress(KeyAscii As Integer)
-
If Len(tPackTX.Text) = 24 Then KeyAscii = 8: Exit Sub
-
End Sub
NeoPa 32,556
Expert Mod 16PB
Using the KeyPress event is a slightly different idea (in as much as using the Change event has already been discussed - which is fundamentally very similar), but your usage of it is not quite correct. To avoid handling the new character you should set KeyAscii to 0. Setting it to 8 (or BS) would ensure you lost the last (24th in this case) character and be left with a 23 char string. - Private Const conMaxXX As Integer = 24
-
-
Private Sub XX_KeyPress(KeyAscii As Integer)
-
If Len(Me.XX.Text) >= conMaxXX Then KeyAscii = 0
-
End Sub
This isn't the same as a simple .MaxLength property, but it should give a similar effect.
Neo Pa, in your solution it is not possible to use backspace when you reach 24 chars, as you set keyascii=0.
You may use my solution to keep 24 chars by checking len(...)=25.
NeoPa 32,556
Expert Mod 16PB
No. The character isn't added until the KeyPress event procedure has been completed as it is used to determine which charater, if any, should be added. Therefore, it doesn't make sense to try to use a backspace - as that would be to clear a character that has never been added.
Did you even try testing this before arguing the point. Especially as the problem has already been pointed out to you.
However, you do make a good point that the code I posted would not handle deleting characters when the maximum length had been reached. The solution is certainly not to play around with adding a backspace character, but could be handled by allowing the Del or Backspace characters through regardless of the limit. - Private Const conMaxXX As Integer = 24
-
-
Private Sub XX_KeyPress(KeyAscii As Integer)
-
If Len(Me.XX.Text) < conMaxXX Then Exit Sub
-
If KeyAscii = vbKeyBack _
-
Or KeyAscii = vbKeyDelete Then Exit Sub
-
KeyAscii = 0
-
End Sub
Phew! Talk about a long conversation... :-)
Seems to me, the simplest thing would be to just chop off the last character, something like the proposal in message #22.
I really don't like the idea of playing around with keypress, as it interferes too much with normal operation of the field. Consider, for example, what would happen if the field is full and I select a few characters then type a letter to replace them. Or I use the mouse to paste in some text which makes the value too long.
Another alternative would be to keep track of the current value in the field. Each time it changes (Change event, I suppose) you check the new value. If it's too long, simply return the previous value. (Note though, if changing a field within that field's change event, one must be wary of creating an infinite loop).
zmbd 5,501
Expert Mod 4TB
I've done variations on theme for keypress, onchange, etc...
The simplist and the one that gives my users the least heartburn is to just tell them in the lable text:
LableTextForField (max10): [(un)BoundControlHere]
then to either just nail it with inputbox as in Post #4 or some variation upon the theme such as Post#22
usually in the afterupdate event.
I do have a couple of fancy textboxes where I have the onchange event keep track of the number of charaters and updates the the builtin status bar with a # of ## thing. However, that's for the fields that have 100 or so character limit.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: wing |
last post by:
Hi all,
I find a JavaScript that limits the field length in a textarea, but it
is not completed.
The script does not handle the copy and paste case. For example, says
the textarea field...
|
by: Shahzad Atta |
last post by:
Hi All,
I am working on a world ready application. I just want to know the best
strategy / strategies to handle the input length property of controls. The
key criteria is that it should be user...
|
by: Jon Hoowes |
last post by:
Hi,
I have inherited some code that has some form elements (radio buttons)
that are called "1", "2" etc.
for example:
<input name="2" type="radio" value="45">
<input name="2" type="radio"...
|
by: danparks |
last post by:
I understand how to use ASP to retrieve a value from a form textbox. I don't understand how to use ASP to fill in a value in a form textbox. I'm guessing that perhaps Response.Write can be used? If...
|
by: Derty |
last post by:
Any way to limit the length of databound text on a datalist
itemtemplate?
|
by: Terry |
last post by:
Hi,
i want to limit input in this textbox up to 2O characters, but i can input a
unlimited amount of characters. How to limit that?
<asp:TextBox ID="txtStreet" runat="server" Width="200px"...
|
by: buddyr |
last post by:
I am scanning a barcode into first textbox on a microsoft access form.
When I scan barcode - the number does not appear in textbox. When I tab to next textbox - it appears. Is there a way to have it...
|
by: John |
last post by:
I am looking for VBA code that will work with Access 2003 to enable
dragging and dropping a file/folder name from Windows XP Explorer into
an Access form's text box. This is a common functionality...
|
by: eko99312 |
last post by:
Dear All,
As you know that input data on excel is easy than access, you can simple click the date that you prefer and input the value that you want. For example :
Consider that the month that I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |