Validation and SetFocus

teknogeek9

Registered User.
Local time
Today, 09:47
Joined
Jan 4, 2001
Messages
15
I have encountered a very strange situation which I am wondering if any of you have an insight to.

I have a form for which the first field is a bounded combobox (MOD_ID) must be filled. The value can either be entered by the user or selected from a table.

In my onExit for this field, I have the following code:
******************************************
Private Sub MOD_ID_Exit(Cancel As Integer)
If IsNull(Me.MOD_ID) Then
MsgBox "Please select a module."
Me.SCRIPT_NBR.SetFocus
Me.MOD_ID.SetFocus
End If
End Sub
******************************************

Notice that I have Me.SCRIPT_NBR.SetFocus before Me.MOD_ID.SetFocus. What this does is forced the cursor to go to the next field and then back to the field which must be filled. (Users don't see this happening but us programmers "do"!)

If I don't include Me.SCRIPT_NBR.SetFocus, the cursor does not go back to MOD_ID. I am actually using Me.SCRIPT_NBR.SetFocus to fool the code to go to MOD_ID.

Any ideas?
 
What is happening is normal .....

You can not setfocus to a control that already has focus ... which is the case in your example.

The thing here is that you are using the wrong event to check the value. You are checking the value after the "horse is out of the barn". You need to check it "before he gets out". Use the On Before Update event of the control to do this instead:

Private Sub MOD_ID_BeforeUpdate(Cancel As Integer)
If IsNull(Me.MOD_ID) Then
MsgBox "Please select a module."
Cancel = True
End If
End Sub

HTH
RDH
 
Actually, under your method, I can still tab out of the field. The point with what I am doing is to actually prevent someone from tabbing out simply by refocusing them back. As for updating the record, I am doing this via command button.
 
Well then ..... use my code with the original event you were using (On Exit). The "Cancel = True" will keep the the cursor from leaving the control.

HTH
RDH
 
This field will be required, no matter what...either for a new record or as a source of a search.
 
Cancel=true works like a charm! Thanks for your help!
Sam
 

Users who are viewing this thread

Back
Top Bottom