Solved

Access 2007 lost focus event

Posted on 2009-07-14
3
1,492 Views
Last Modified: 2013-11-28
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.
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

Open in new window

0
Comment
Question by:ochness
3 Comments
 
LVL 39

Expert Comment

by:thenelson
ID: 24855299
After the lost focus event completes, the keystroke that moved the focus (Tab, Enter, etc) or the mouse click completes. This occurs after the CustId.SetFocus command. So that command has to be placed outside of and after the lost focus event. You can use the form timer event for this:

Private Sub CustId_LostFocus()
 
Dim varLength As Variant
varLength = Len(CustId.Value)
 
If IsNull(varLength) = True Then
    MsgBox ("You must enter a Cust ID.")
    Me.TimerInterval = 1
    Exit Sub
End If
 
End Sub

Private Sub Form_timer()
Me.TimerInterval = 0
CustId.SetFocus
End Sub
0
 
LVL 16

Accepted Solution

by:
Chuck Wood earned 50 total points
ID: 24858809
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

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

Open in new window

0
 
LVL 1

Author Closing Comment

by:ochness
ID: 31603435
This was so long ago, but  I believe this did the trick.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question