[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2328
  • Last Modified:

How to effectively cancel LostFocus event in vba

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
shifty_mc
Asked:
shifty_mc
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<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
 
shifty_mcAuthor Commented:
Beautiful stuff - simple as that.

Cheers - points given.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now