View Full Version : Locking the focus of a control until some data is entered in that control


mark curtis
12-16-2000, 08:29 AM
Pat Hartman
Member posted 12-12-2000 07:37 PM
--------------------------------------------------------------------------------
You have a choice. You can write a lot of code or you can write a small amount of code. My personal choice is as little code as possible. That way I don't have to write it. I don't have to test it. And, I don't have to debug it.
In the BeforeUpdate event of the FORM put code that checks the field in question for a valid value. If it has none, cancel the update event, display a message box, and set the focus back to the field you want them to enter data into.

If IsNull(YourFld) or Len(Trim(YourFld)) = 0 Then
Cancel = True
MsgBox "Please enter data in YourFld before proceeding", VBOKOnly
Me.YourFld.SetFocus
End If

Your alternative is to put similar code in EVERY control on the form! Or, even worse, enabling and disabling controls one by one to force them into a specific navigation path through the data entry. UGH!


mark curtis
Junior Member posted 12-12-2000 11:51 PM
--------------------------------------------------------------------------------
Thanks for the replies but still have a slight problem that when I close my form the message box is still visible and I have to press it 4 times to make it go. Any clues???
Thanks

Atomic Shrimp
12-18-2000, 12:43 AM
That sounds to me more like you somehow have 4 message boxes open all on top of each other.

Could you post your code?

Mike

Pat Hartman
12-18-2000, 06:28 PM
It's probably the setfocus that is causing the problem. Try commenting it out. If that works, you can either live without setting focus back to the missing field or do some fancier coding to keep track of the number of times you have entered this subroutine. On the second (or greater) time through you can skip the msgbox and setfocus statements and just cancel the update event without any user notification.

If commenting out the setfocus does not eliminate the problem you'll need to put a stop on the msgbox so you can step through the code to see if you can identify what is causing the loop.