By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

UnBound Combo Box Behaviour

P: n/a
I have an unbound combo box in the form header. I have used an input
mask "CCCC" 40 times to limit the max number of characters to 40.

When I tab into the combo box & press a character say "K" for example,
the entire first match gets filled in the combo box and the cursor
goes to the last character of the combo box. So I can't effectively
search for a "KI" until I backspace the entire entry which has got
filled in.

Once i remove the input mask the combo box behaves normally. However I
need the input mask to limit the max number of characters input to 40.
Also, the combo box "limit to list" property is set to false so the
user can select an existing value from the combo box + add/delete/
modify the combo box contents.

Can anyone please help.

Thx & Best Rgds,
Prakash.
Mar 5 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Obviously, the interaction with the Input Mask is the cause of your problem.

There are other ways to limit the number of characters... you can put a test
for length in the BeforeUpdate event and cancel the update if it is too
long. Some of the other ways you could use can be confusing, at best, and
irritating at worst, with a Combo, so I don't suggest those -- but if
catching the over-lengthy entry after it is too long is not to your liking,
you could search the newsgroup archives on KeyPress for a different
approach.

There are other, more complex approaches that one might use to try to tailor
the user experience, but they will lose the built-in "AutoExpand" capability
that lets you search the list so easily.

Larry Linson
Microsoft Office Access MVP
<pr*************@gmail.comwrote in message
news:22**********************************@s12g2000 prg.googlegroups.com...
>I have an unbound combo box in the form header. I have used an input
mask "CCCC" 40 times to limit the max number of characters to 40.

When I tab into the combo box & press a character say "K" for example,
the entire first match gets filled in the combo box and the cursor
goes to the last character of the combo box. So I can't effectively
search for a "KI" until I backspace the entire entry which has got
filled in.

Once i remove the input mask the combo box behaves normally. However I
need the input mask to limit the max number of characters input to 40.
Also, the combo box "limit to list" property is set to false so the
user can select an existing value from the combo box + add/delete/
modify the combo box contents.

Can anyone please help.

Thx & Best Rgds,
Prakash.

Mar 5 '08 #2

P: n/a
On Mar 5, 11:02*am, "Larry Linson" <boun...@localhost.notwrote:
Obviously, the interaction with the Input Mask is the cause of your problem.

There are other ways to limit the number of characters... you can put a test
for length in the BeforeUpdate event and cancel the update if it is too
long. Some of the other ways you could use can be confusing, at best, and
irritating at worst, with a Combo, so I don't suggest those -- but if
catching the over-lengthy entry after it is too long is not to your liking,
you could search the newsgroup archives on KeyPress for a different
approach.

There are other, more complex approaches that one might use to try to tailor
the user experience, but they will lose the built-in "AutoExpand" capability
that lets you search the list so easily.

*Larry Linson
*Microsoft Office Access MVP
Larry,

I agree that using a method other than the Input Mask for limiting the
length of the text in the combobox is good advice. Perhaps you missed
the word "unbound" or use bound forms exclusively. The BeforeUpdate/
Cancel is, if I remember correctly, a common way to validate bound
forms. Typically, validation for unbound forms is done in the
AfterUpdate event. However, the method I use for that purpose
validates in neither event.

Here's what I use to limit the length of entries in comboboxes:

'code behind form
Private Sub cbxComboName_KeyPress(KeyAscii As Integer)
KeyAscii = LimitCombo(KeyAscii, cbxComboName, 50)
End Sub

'module code
Public Function LimitCombo(KeyAscii As Integer, cbxCombo As ComboBox,
intMax As Integer) As Integer
'If the field has reached its maximum then return 0
LimitCombo = KeyAscii
cbxCombo.SetFocus
If Len(cbxCombo.Text) = intMax Then
If KeyAscii <8 Then LimitCombo = 0
End If
End Function

That will limit the contents of the combobox to 50 characters. If the
50 character limit is reached, the combobox ignores any more text.
Letting KeyAscii = 8 through allows the user to delete the last
character if they reach the limit and made a mistake on the final
character. This method should work on bound forms also and preserve
AutoExpand, but check to be sure.

James A. Fortune
CD********@FortuneJames.com
Mar 5 '08 #3

P: n/a
On Mar 5, 2:08*pm, CDMAPos...@fortunejames.com wrote:
However, the method I use for that purpose validates in neither event.
I just tried pasting in text that was longer than the limit, so
perhaps use the method in combination with your normal validation
(i.e., check the length there also) or preclude ways to get around the
method's limitations.

James A. Fortune
CD********@FortuneJames.com
Mar 5 '08 #4

P: n/a
On Mar 5, 11:20 pm, CDMAPos...@fortunejames.com wrote:
On Mar 5, 2:08 pm, CDMAPos...@fortunejames.com wrote:
However, the method I use for that purpose validates in neither event.

I just tried pasting in text that was longer than the limit, so
perhaps use the method in combination with your normal validation
(i.e., check the length there also) or preclude ways to get around the
method's limitations.

James A. Fortune
CDMAPos...@FortuneJames.com

I'm sorry James, but please excuse my lack of Access knowledge. I sort
of understood the code of your previous post, but how do I check the
length when the user has pasted code into the combo box ? In which
event would I test and how ?

Thx for the help,
Prakash.
Mar 6 '08 #5

P: n/a

<pr*************@gmail.comwrote in message
news:ee**********************************@m34g2000 hsc.googlegroups.com...
On Mar 5, 11:20 pm, CDMAPos...@fortunejames.com wrote:
>On Mar 5, 2:08 pm, CDMAPos...@fortunejames.com wrote:
However, the method I use for that purpose validates in neither event.

I just tried pasting in text that was longer than the limit, so
perhaps use the method in combination with your normal validation
(i.e., check the length there also) or preclude ways to get around the
method's limitations.

James A. Fortune
CDMAPos...@FortuneJames.com


I'm sorry James, but please excuse my lack of Access knowledge. I sort
of understood the code of your previous post, but how do I check the
length when the user has pasted code into the combo box ? In which
event would I test and how ?
Jim's correct -- the AfterUpdate event is the proper place for the code. The
following code works for me, to replace however many characters are typed or
pasted into the Combo Box with just the first five. (My clients do not
expect their applications to "coddle" their users by being overly
solicitous -- if there's a limit, it does not have to be detected at the
first keystroke beyond that limit. They figure, I am sure, that typing
several extra characters and having them disappear is an incentive for the
user to learn and abide by the limit if they want to avoid the extra
typing.)

The AfterUpdate event does not fire until you press enter, or tab, or click
out of the Combo Box.

Private Sub cboName_AfterUpdate()
If Len(cboName.Text) 5 Then
Me.cboName = Left(Me.cboName.Text, 5)
End If
End Sub

Larry Linson
Microsoft Office Access MVP
Mar 6 '08 #6

P: n/a
On Mar 5, 10:13*pm, prakashwadhw...@gmail.com wrote:
I'm sorry James, but please excuse my lack of Access knowledge. I sort
of understood the code of your previous post, but how do I check the
length when the user has pasted code into the combo box ? *In which
event would I test and how ?

Thx for the help,
Prakash.
Larry is correct about the event and the viability of his psychology.
The way the code I posted works is that when the limit is reached, the
user hits a metaphorical wall. A KeyAscii value of zero gets ignored
and all the other KeyAscii values get through. Thus, when the limit
is reached, all further keystrokes get ignored except for the
backspace character, whose ASCII value is 8. This prevents the user
from typing more characters than the field allows, but does not
prevent entering too many characters via other means. The editing
doesn't fire the AfterUpdate event until the editing is complete. If
someone gets around the system by pasting in more characters than the
limit, the AfterUpdate event can catch it then. Catching the length
at the keystroke level is a little awkward until users get used to
it. Using Larry's idea of simply using the AfterUpdate event isn't
perfect either. Maybe the KeyPress event could be used to pop up a
warning when the limit is reached that specifies the number of
characters allowed in the field instead of erecting a wall. The
AfterUpdate code can stay in case they flaunt the warning or try to
get around the system. It is also possible to code yet more events in
attempt to close all the loopholes, but usually a workable arrangement
can be found before that point is reached.

James A. Fortune
CD********@FortuneJames.com
Mar 6 '08 #7

P: n/a
On Mar 7, 12:38 am, CDMAPos...@fortunejames.com wrote:
On Mar 5, 10:13 pm, prakashwadhw...@gmail.com wrote:
I'm sorry James, but please excuse my lack of Access knowledge. I sort
of understood the code of your previous post, but how do I check the
length when the user has pasted code into the combo box ? In which
event would I test and how ?
Thx for the help,
Prakash.

Larry is correct about the event and the viability of his psychology.
The way the code I posted works is that when the limit is reached, the
user hits a metaphorical wall. A KeyAscii value of zero gets ignored
and all the other KeyAscii values get through. Thus, when the limit
is reached, all further keystrokes get ignored except for the
backspace character, whose ASCII value is 8. This prevents the user
from typing more characters than the field allows, but does not
prevent entering too many characters via other means. The editing
doesn't fire the AfterUpdate event until the editing is complete. If
someone gets around the system by pasting in more characters than the
limit, the AfterUpdate event can catch it then. Catching the length
at the keystroke level is a little awkward until users get used to
it. Using Larry's idea of simply using the AfterUpdate event isn't
perfect either. Maybe the KeyPress event could be used to pop up a
warning when the limit is reached that specifies the number of
characters allowed in the field instead of erecting a wall. The
AfterUpdate code can stay in case they flaunt the warning or try to
get around the system. It is also possible to code yet more events in
attempt to close all the loopholes, but usually a workable arrangement
can be found before that point is reached.

James A. Fortune
CDMAPos...@FortuneJames.com


Larry & James ... thank you very much for your replies. Sorry, I was
posted in the interior during the past week & could not reply.

If the user has exceeded an input length of 40 characters, I'd like to
flash a message via a msgbox & set focus back to that combo box.
However, much as I try, the focus moves back to the next control. Is
there anything I'm doing wrong here ?

Here's my code:

Private Sub txt_RcdFm_PdTo_AfterUpdate()
If Len(txt_RcdFm_PdTo.Text) 40 Then
MsgBox "Length of 'PAID TO' cannot exceed 40 characters"
Me.txt_RcdFm_PdTo = Left(Me.txt_RcdFm_PdTo.Text, 40) '
SELECT characters exceeding 40 here
Me.txt_RcdFm_PdTo.SetFocus
End If
End Sub
What I'd like now is the cursor should go back to txt_RcdFm_PdTo and
the number of characters exceeding 40 should be SELECTED. i.e. if the
user has entered 55 characters, the last 15 characters should be
highlighted.

I think I might just have to use the beforeupdate event here.

Thx for all help.

Rgds,
Prakash.
Mar 11 '08 #8

P: n/a
On Mar 11, 11:43*am, prakashwadhw...@gmail.com wrote:
Larry & James ... thank you very much for your replies. Sorry, I was
posted in the interior during the past week & could not reply.

If the user has exceeded an input length of 40 characters, I'd like to
flash a message via a msgbox & set focus back to that combo box.
However, much as I try, the focus moves back to the next control. Is
there anything I'm doing wrong here ?

Here's my code:

Private Sub txt_RcdFm_PdTo_AfterUpdate()
* * *If Len(txt_RcdFm_PdTo.Text) 40 Then
* * * * MsgBox "Length of *'PAID TO' *cannot exceed 40 characters"
* * * * Me.txt_RcdFm_PdTo = Left(Me.txt_RcdFm_PdTo.Text, 40) *'
SELECT characters exceeding 40 here
* * * * Me.txt_RcdFm_PdTo.SetFocus
* * *End If
End Sub

What I'd like now is the cursor should go back to *txt_RcdFm_PdTo *and
the number of characters exceeding 40 should be SELECTED. i.e. if the
user has entered 55 characters, the last 15 characters should be
highlighted.

I think I might just have to use the beforeupdate event here.

Thx for all help.

Rgds,
Prakash.
Use the SelStart and SelLength properties of the textbox after using
SetFocus, to highlight the text you want.

Something like (i.e., air code):

Private Sub txt_RcdFm_PdTo_AfterUpdate()
Const PdMAX = 40
If Len(Nz(Me!txt_RcdFm_PdTo.Value, "")) PdMAX Then
'omit Me.txt_RcdFm_PdTo = Left( ... so you have something to select
With Me!txtRcdFm_PdTo
.SetFocus
.SelStart = PdMAX + 1
.SelLength = Len(.Value) - PdMAX
End With
End If
End Sub

James A. Fortune
CD********@FortuneJames.com
Mar 11 '08 #9

P: n/a
On Mar 11, 9:40 pm, CDMAPos...@fortunejames.com wrote:
On Mar 11, 11:43 am, prakashwadhw...@gmail.com wrote:
Larry & James ... thank you very much for your replies. Sorry, I was
posted in the interior during the past week & could not reply.
If the user has exceeded an input length of 40 characters, I'd like to
flash a message via a msgbox & set focus back to that combo box.
However, much as I try, the focus moves back to the next control. Is
there anything I'm doing wrong here ?
Here's my code:
Private Sub txt_RcdFm_PdTo_AfterUpdate()
If Len(txt_RcdFm_PdTo.Text) 40 Then
MsgBox "Length of 'PAID TO' cannot exceed 40 characters"
Me.txt_RcdFm_PdTo = Left(Me.txt_RcdFm_PdTo.Text, 40) '
SELECT characters exceeding 40 here
Me.txt_RcdFm_PdTo.SetFocus
End If
End Sub
What I'd like now is the cursor should go back to txt_RcdFm_PdTo and
the number of characters exceeding 40 should be SELECTED. i.e. if the
user has entered 55 characters, the last 15 characters should be
highlighted.
I think I might just have to use the beforeupdate event here.
Thx for all help.
Rgds,
Prakash.

Use theSelStartand SelLength properties of the textbox after using
SetFocus, to highlight the text you want.

Something like (i.e., air code):

Private Sub txt_RcdFm_PdTo_AfterUpdate()
Const PdMAX = 40
If Len(Nz(Me!txt_RcdFm_PdTo.Value, "")) PdMAX Then
'omit Me.txt_RcdFm_PdTo = Left( ... so you have something to select
With Me!txtRcdFm_PdTo
.SetFocus
.SelStart= PdMAX + 1
.SelLength = Len(.Value) - PdMAX
End With
End If
End Sub

James A. Fortune
CDMAPos...@FortuneJames.com

Thx James for the invaluable advice & code. Somehow though after
fiddling around, I opted to go with Allen Browne's code from his site:
http://allenbrowne.com/ser-34.html

I think it makes the Access unbound Text Box behave almost like a
bound text box.

What still beats me is why don't the MS developers make a small
property for an unbound textbox which would enable the user to specify
the maximum length of a string. eg: txt_MyTextbox.MaxLength=40

Am I wrong in thinking along these lines ?

Thanks though to all who have helped.

Best Rgds,
Prakash.
Mar 19 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.