By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,509 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,509 IT Pros & Developers. It's quick & easy.

How do I make the subform the active control when the mouse moves over it?

Seth Schrock
Expert 2.5K+
P: 2,931
I've got a form that has a subform on it. Currently, in order to scroll in the subfomr with the mouse wheel, I have to click somewhere in the subform. I would like to be able to be able to have it be selected when the mouse moves over it so that I don't have to click. I've looked at the subform events, but the only two listed are On_Enter and On_Exit. I tried looking at the events of the subform as a form and not as a subform and the only one that makes sense is On_Mouse_Move. The only problem is that I don't know how to check if the mouse is moving over the subform or the regular form.

Is this possible?
Feb 8 '12 #1

✓ answered by Mihail

@ADezii
Correct me, please if I am wrong.
I think that in boolean logic your counter act the same way as a boolean variable: Your counter IS or IS_NOT = 1.

@Seth
Your code can't work (either if you use a boolean variable or a counter one) because you initialize SubFormHasFocus inside the event procedure (line 2). And the default value is FALSE.
So, the IF statement will be executed every time your mouse is moving.
Line 8 has no effect. You can remove it :). That because when you exit from procedure (line 10) all variables declared inside the procedure using Dim statement are complete removed from memory.
If you wish to keep a value for a variable between calling you must use the Static statement. Simple replace DIM with STATIC.

In my first post I say to define the boolean variable at MODULE level.
Also your line 8 must be moved between line 5 and 6.

Any way, I prepare an example (see attachment) with a possible approach to solve your original question:
How do I make the subform the active control when the mouse moves over it?

Hope this is a help for you.
Good luck !

Share this Question
Share on Google+
17 Replies


ADezii
Expert 5K+
P: 8,600
Try the MouseMove() Event in the Detail Section of the SubForm.
Feb 8 '12 #2

Seth Schrock
Expert 2.5K+
P: 2,931
Okay, I put the following in the MouseMove() event like you said.
Expand|Select|Wrap|Line Numbers
  1. Me.Parent![Returned Mail Subform].SetFocus
The only problem is that as long as the mouse is over the detail section (it doesn't have to be moving), it keeps running over and over. Is there a way to keep it from getting triggered when the mouse isn't moving?
Feb 8 '12 #3

NeoPa
Expert Mod 15k+
P: 31,186
I find your explanation very confusing Seth. Can you post the code (including all relevant lines, such as the procedure wrappers, etc).
Feb 8 '12 #4

100+
P: 759
Define a boolean variable at the module level
Expand|Select|Wrap|Line Numbers
  1. Dim SubFormHasFocus As Boolean
Set it to TRUE after the sub-form recive focus and run your code only if this variable is FALSE.
When the sub-form lose focus set the variable to FALSE.
Feb 9 '12 #5

Seth Schrock
Expert 2.5K+
P: 2,931
I didn't get a chance to work on it today. I'll post more tomorrow.
Feb 9 '12 #6

NeoPa
Expert Mod 15k+
P: 31,186
No worries Seth. I'm out most of tomorrow but I'll catch up when I'm around certainly.
Feb 10 '12 #7

Seth Schrock
Expert 2.5K+
P: 2,931
@NeoPa, Here is the code. It is in the subform's Detail on mouse move event.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. Me.Parent![Returned Mail Subform].SetFocus
  3. End Sub
  4.  
@Mihail, Here is the code that I tried per your idea:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. Dim SubFormHasFocus As Boolean
  3.  
  4. If SubFormHasFocus = False Then
  5.     Me.Parent![Returned Mail Subform].SetFocus
  6. End If
  7.  
  8. SubFormHasFocus = True
  9.  
  10. End Sub
However, the screen keeps cycling whenever the the mouse is over the detail section. If I put my mouse over a control, it stops. I'm also concerned about performance since this is the main screen used. This code will be run over and over even if we get the SetFocus problem fixed. Is there an event that runs when the mouse first moves over the subform instead of it running all the time the mouse is over the subform?
Feb 10 '12 #8

ADezii
Expert 5K+
P: 8,600
Instead of a BOOLEAN, try a Counter (LONG). Increment the Counter by +1 whenever the Mouse moves acccross the Detail Section of the Sub-Form. Only set the Focus to the Sub-Form when the Value of the Counter = 1, then Reset the Counter to 0 when the Mouse moves to the Main Form.
Feb 10 '12 #9

100+
P: 759
@ADezii
Correct me, please if I am wrong.
I think that in boolean logic your counter act the same way as a boolean variable: Your counter IS or IS_NOT = 1.

@Seth
Your code can't work (either if you use a boolean variable or a counter one) because you initialize SubFormHasFocus inside the event procedure (line 2). And the default value is FALSE.
So, the IF statement will be executed every time your mouse is moving.
Line 8 has no effect. You can remove it :). That because when you exit from procedure (line 10) all variables declared inside the procedure using Dim statement are complete removed from memory.
If you wish to keep a value for a variable between calling you must use the Static statement. Simple replace DIM with STATIC.

In my first post I say to define the boolean variable at MODULE level.
Also your line 8 must be moved between line 5 and 6.

Any way, I prepare an example (see attachment) with a possible approach to solve your original question:
How do I make the subform the active control when the mouse moves over it?

Hope this is a help for you.
Good luck !
Attached Files
File Type: zip SubFormSetFocus.zip (27.3 KB, 149 views)
Feb 10 '12 #10

Seth Schrock
Expert 2.5K+
P: 2,931
That is exactly what I need.

I totally missed the at module level. I'll admit, I'm not very good with VBA. I have a book that would teach me if I read it, but I just don't have the time:) I've learned a lot through this forum, but I wish I new more. I'm basically learning the stuff that I need and then I print out the code so that I can use it next time with the proper adjustments.
Feb 10 '12 #11

Seth Schrock
Expert 2.5K+
P: 2,931
I just put the label around my subform and did the OnMouseMove event. What I don't get is that on my database, when my mouse is over the label, the code is constantly running even when the mouse is not moving. In your sample it doesn't run unless the mouse is moving and only once every half inch of movement. Any ideas? I'm using Access 2010.
Feb 10 '12 #12

100+
P: 759
And where is the problem ? Hope you had removing the BEEP statement from my code. It is not necessary. So the code can run in silence how long your mouse is over the label.

If this is a really problem for you, let me know and I am sure that can be solved. But please tell me what trouble you have if the code is running when your mouse is over the label.
Feb 10 '12 #13

Seth Schrock
Expert 2.5K+
P: 2,931
The problem is that the screen flashes at about 5 flashes per second. Admittedly, there is only a little area on the screen where the mouse can be where it will do this. The code works fine in that the subform is selected. Your sample works perfectly without the flashing. I just don't know why mine works differently from yours. I did remove the beep portion of the code. I'm no longer at work, so I don't have access to the code until Monday.
Feb 10 '12 #14

100+
P: 759
I think my code also flash but is a little bit to screen refresh so it is not visible that flash.
Ok. I'll see until Monday how can be improved.

Have a nice week-end !
Feb 10 '12 #15

100+
P: 759
Replace the code in MY database with this one:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Label_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.     If Not (ActiveControl.Name = "Subform") Then
  3.         Beep
  4.         Subform.SetFocus
  5.         Txt = "The Subform HAS focus. Click the command button."
  6.     End If
  7. End Sub
then adapt for you.
Feb 10 '12 #16

Seth Schrock
Expert 2.5K+
P: 2,931
I forgot that I needed to test out your code! Anyway, I just tried it on yours and it didn't flash. I adapted it for my database and it flashed. So I decided to do a test and commented out all of the code and it still flashed. Another thing that I noticed is that the VBA editor says that mine is constantly running whereas yours does not. So I think that it has something to do with my database. Thanks for your help.
Feb 15 '12 #17

100+
P: 759
Hm.
Try to copy-paste MY label from MY form into your form.
Or import MY form in your database and work with it.

I can't see any reason for your trouble.
Sorry.
Feb 15 '12 #18

Post your reply

Sign in to post your reply or Sign up for a free account.