Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SetFocus after validate textbox, not working

Posted on 2009-05-05
4
Medium Priority
?
515 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 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 free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
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…

971 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