Mousedown event for frame control does not always fire

JohnM77

New Member
Joined
Feb 4, 2016
Messages
1
Hoping someone can help with this - I am trying to develop a class for userform textbox validation using events. I would like to prevent the user from exiting the textbox if the textbox text is not valid (such as blank or non-numeric value, etc.) for whatever reason. This would normally be done in the textbox exit event (using the Cancel argument), but the lack of the exit event for a class that contains a textbox defined WithEvents is well documented, so I am trying to take a different approach.

Instead of preventing the focus from leaving the offending textbox, I am trying to detect movement of the focus to a new control and then setting the focus back to the textbox. I am making progress, but have encountered a problem with the frame control. Here is a simple case - assume Textbox1 is placed directly on the form and Textbox2 is located inside a frame control. If TextBox1's text is invalid (blank for this simple example), I would like to set the focus back to it when the user clicks on the frame control. So I put code in the frame's MouseDown event to set the focus back to TextBox1 if it's text is blank.

This works well (focus returns to TextBox 1) if I click in an area that is clearly inside the frame control. However, if I click just inside the frame border or on the frame caption area, the focus sometimes moves to the frame control. I determined that this occurred because the MouseDown event did not occur in these cases. I found MouseUp had the same problem.

But then I discovered that using the frame's Enter event produced the desired result - focus always bounced back to TextBox1, no matter where I click on the frame control. This is fine, except that the frame's Enter event code must be done at design time, since the enter event is also not available for a frame declared WithEvents in a class. This is annoying and I would like to be able to use MouseDown instead, but cannot solve this problem. I get the same result in Excel 2003, 2010, and 2013. Does anyone have any idea why the MouseDown and MouseUp events are sometimes missing as I described?
 

Forum statistics

Threads
1,081,562
Messages
5,359,614
Members
400,540
Latest member
JimUSMC

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top