Question : Access 2007 lost focus event

I am trying to use the Lost Focus event to validate if a field is null or not. After I display the message box I want to set the focus back on the same field, but the code I have moves to the next field.

I have attached the code. Any assistance in pointing out what I'm doing wrong would be great.

Thanks.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Private Sub CustId_LostFocus()
 
Dim varLength As Variant
varLength = Len(CustId.Value)
 
If IsNull(varLength) = True Then
    MsgBox ("You must enter a Cust ID.")
    CustId.SetFocus
    Exit Sub
End If
 
End Sub

Answer : Access 2007 lost focus event

You can use the Enter event of the control the form moves to after CustId (called NextControl in the snippet) to trigger the event. Also, you can use the value of the field to test for nulls and just spaces and zero-length strings as shown in the snippet, without introducing another variable.
Chuck
1:
2:
3:
4:
5:
6:
7:
8:
Private Sub NextControl_Enter()
    ' If CustId is blank,
    If IsNull(CustId.Value) Or Len(Trim(CustId.Value)) = 0 Then
        ' warn the user and set the focus to CustId
        MsgBox ("You must enter a Cust ID.")
        CustId.SetFocus
    End If
End Sub
Random Solutions  
 
programming4us programming4us