Solved

How to effectively cancel LostFocus event in vba

Posted on 2004-09-14
2
2,064 Views
Last Modified: 2012-05-05
Hi,

I have some code that has to be run in the LostFocus event of a textbox.

It checks the contents, and if they're not ok according to some rules, it sets the focus back on the textbox and higlights the text...
If ... Then
        MsgBox "The UAN you entered is not valid, please retype", vbCritical, "Invalid UAN"
        Cancel = True
        Me.B_C.SelStart = 0
        Me.B_C.SelLength = Len(Me.B_C.Value)
End If

That works fine in the BeforeUpdate event where cancel is a parameter, and I understand why it won't work in the LostFocus event, but is there some kind of workaround? ie would it work if I abstracted the code to a module or something along those lines?

Thanks for your help
0
Comment
Question by:shifty_mc
2 Comments
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 75 total points
ID: 12052882
<<but is there some kind of workaround?>>

 Use the OnExit event rather then LostFocus.  LostFocus is too late.  Access is already moving to another control.

Jim.
0
 

Author Comment

by:shifty_mc
ID: 12052935
Beautiful stuff - simple as that.

Cheers - points given.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

860 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