Solved

Access 2007 lost focus event

Posted on 2009-07-14
3
1,484 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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…

809 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