Solved

Access 2007 lost focus event

Posted on 2009-07-14
3
1,504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

691 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