View Full Version : On Lost Focus


Haytham
01-19-2002, 04:03 AM
Hi All...
I have a required field in my table.
I don't want the user to skip entering any data in there.
So In On Lost Focus:

If IsNull (Me.FieldName) Then
MsgBox ""
DoCmd.Cancelevent
Else
..............
End If
But still this field can be skipped smoothly.
I tried Before and After Update but still the same
Any help please...
Thank you

jwindon
01-19-2002, 05:53 AM
Need to say Exit Sub instead of DoCmd.CancelEvent and reset the field to NULL.

If IsNull (Me.FieldName) Then

MsgBox "Required field"
Me.FieldName = ""
Exit Sub

Else
..............
End If

I would suggest you use the code on the BeforeUpdate event of the form versus the LostFocus.

The event prevents the user from saving the record.In that case you will want to add the command to cancel the event before leaving the subroutine.


[This message has been edited by jwindon (edited 01-19-2002).]

Haytham
01-19-2002, 11:53 AM
Hi jwindon,
I tried BeforeUpdate, but it gives the same result. i.e. the field can be skipped without entry.

Jack Cowley
01-19-2002, 01:40 PM
Did you put your code in the FORMS Before Update event as JWindon suggested?

Haytham
01-19-2002, 01:43 PM
Hi Jack, Yes I put the code in BeforeUpdate of my Field, but still it can be skipped without entry.
Only MsgBox will be displayed.

jwindon
01-19-2002, 06:34 PM
Haytham:

Not the field's event, the form's BeforeUpdate event ......

Did you put in Exit Sub as I suggested?....


If IsNull (Me.FieldName) Then
MsgBox "Required field"
Me.FieldName = ""
DoCmd.CancelEvent
Exit Sub

Else
..............
End If

Haytham
01-20-2002, 06:22 AM
Hi jwindon,
I tried the code On Form Level Before Update, but still nothing new.
I'll keep on trying
db can be sent if needed...
Thanks a lot.

jwindon
01-20-2002, 06:31 AM
Haytham:

Sent you a demo. Could it be that the fieldname you are checking is a reserved word in Access such as Me.Name?

Here is the code in my demo for others:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.PersonName) Then

MsgBox "This Information is required"
Me.PersonName.SetFocus
DoCmd.CancelEvent
Exit Sub

Else


MsgBox "Continue code from here..."

End If

End Sub

[This message has been edited by jwindon (edited 01-20-2002).]

D B Lawson
01-20-2002, 09:18 AM
You could try

If IsNull (Me.fieldname) or Me.fieldname = 0 Then
MsgBox "*****"
Me.fieldname.setfocus
Exit sub
End If

If it's a numeric field then the 0 is preset and the code will skip past it because, technically, it's not null.

Haytham
01-20-2002, 12:00 PM
Dear jwindon, Thank you for your attachment.
It works well.
I was thinking that there will be a way not to move to next field if you don't enter anything in the specific field. This will do the job. Highly appreciated.
D B Lawson, Thank a lot for answer. I t works well as well.

jwindon
01-20-2002, 01:26 PM
You can set the field to required at the table level or go ahead and put that code on the BeforeUpdate event of the field.

Don't encourage using LostFocus in the case that this form is not data entry yes and that when tabbing thru a record, the code will fire unnecessarily.

Rich
01-20-2002, 01:38 PM
The code's better either being attached to a custom close button, with a message box giving users the option of undoing or returning to the form or the other option is to use the controls tag property which is more reliable when multiple records can be added.
HTH

Haytham
01-20-2002, 02:05 PM
jwindon, Rich .. Thanks a lot.