?
Solved

SetFocus after validate textbox, not working

Posted on 2009-05-05
4
Medium Priority
?
513 Views
Last Modified: 2013-11-28
Hi Guys

For some reason SetFocus is not working, it goes to the next control

Private Sub DateOfBirth_AfterUpdate()
If Not IsNull(Me.DateOfDeath) Then
    If Me.DateOfBirth >= Me.DateOfBirth Then
        MsgBox "Date of Birth must be smaller than Date of Death", , "Date Of Birth"
        Me.DateOfBirth.Value = ""
        Me.DateOfBirth.SetFocus
    End If
End If
End Sub

Any thoughts

BEst regards
0
Comment
Question by:titorober23
[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
  • 2
  • 2
4 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 150 total points
ID: 24306597
To do a validation check like this, you need to use the BEforeupdate event:

Private Sub DateOfBirth_BeforeUpdate(Cancel as Integer)
If Not IsNull(Me.DateOfDeath) Then
    If Me.DateOfBirth >= Me.DateOfBirth Then
        MsgBox "Date of Birth must be smaller than Date of Death", , "Date Of Birth"
        cancel = true
        Me.DateOfBirth.Value = ""
       
    End If
End If
End Sub
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 450 total points
ID: 24306629
This

 If Me.DateOfBirth >= Me.DateOfBirth Then

doesn't make any sense.  It will always be true.  It's the same field.

mx
0
 
LVL 61

Expert Comment

by:mbizup
ID: 24306964
Good catch...

The message box has the description right for the validation check, but the code should be comparing DateOfBirth with DateOfDeath.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 450 total points
ID: 24307029
Also ... as a side note ... I see this at lot being posted by experts et.al:

Me.DateOfBirth.Value = ""    ' (setting to empty string).

However, if that field in the table has its Allow Zero Length String property set to NO (which it SHOULD - see reference below, although sadly, the Default is YES) ... this is going to cause an error.  

However, this:

Me.DateOfBirth.Value = Null

will never cause an error.

Here is the reference to the ZLS issue:

http://allenbrowne.com/bug-09.html

scroll down to Zero Length String

mx
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
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…

718 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