Solved

Access 2007 lost focus event

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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 developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now