Solved

SetFocus after validate textbox, not working

Posted on 2009-05-05
4
511 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 50 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 150 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 150 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…

627 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