Solved

Access 2007 lost focus event

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

920 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