Solved

SetFocus after validate textbox, not working

Posted on 2009-05-05
4
506 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
  • 2
  • 2
4 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 50 total points
Comment Utility
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 - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 150 total points
Comment Utility
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
Comment Utility
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 - Access MVP) earned 150 total points
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

728 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

10 Experts available now in Live!

Get 1:1 Help Now