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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Setfocus question

Status
Not open for further replies.

azzi2000

MIS
Jan 28, 2004
145
0
0
US
The form consists of several entries.

A- I would like to limit the user entry strokes not to exceed 7 numeric entries format 9999.99)
Also on event procedure onExit i have the followinfg code:
if me.cCharge > 9999.99 then
msgbox "Out Of Range - Must be between 0 and 9999.99"
Me.cCharge.SetFocus
End if
It shoud return the focus in CCharge but it is going to the next entry.
Please advice,
Thank you.
Dré
 
It can't set focus to itself while it has focus. The dirty trick, is to setfocus to another control first:

[tt]me!txtSomeOtherControl.setfocus
Me!cCharge.SetFocus[/tt]

The "proper" way, would be to use the before update event of the control (or perhaps before update event of the form?) for the validation. Use Cancel = True to cancel the event.

Roy-Vidar
 
You were absolutely correct.
It worked. Thank you.
How about limiting the number of numeric entry for a numeric field. (ie: limit to 7 digits)
Thank you.
Dré
 
There are a couple of ways, but you haven't made it completely clear what your requirements are. If you whish to completely disallow some sets of characters (special characters, space, letters) when entering data, the keydown event of the control comes to mind.

[tt]select case keycode
case vbkeynumpad0 to vbkeynumpad9, vbkey0 to vbkey9, vbkeydecimal
' if control prior to entering this key, alredy has
' a length of 8, disallow new number
if len(me!txtControl.Text) >= 8 then
keycode=0 ' cancel keystroke
end if
case vbkeydelete, vbkeyback, vbkeyleft, vbkeyright, vbkeytab
' do nothing, they're allowed
case else ' disable the rest
keycode=0 ' cancel keystroke
end select[/tt]

- typed not tested...

Now, this is just a very crude setup, you might want to investigate also whether or not a decimal separator is within the number (then test for different lengths), the vbkeydecimal, on my setup, allows only the numeric keypads decimalseparator, perhaps add 190 to the test, which is the keycode returned for both comma and dot on my setup (wouldn't be surprised, though, if it culd contain other legal characters too, but an isnumeric test would probably take care of that).

Roy-Vidar
 
Royvidar:
And I thought Microsoft would make it much easier than that.
Thank you.
Dré
 
Have you considered using an input mask?

Either on the form for the combo box, ####.##
OR better yet, on the field within the table design.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top