Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling Autoscroll wheel on Mouse? 9

Status
Not open for further replies.

nstouffer

Programmer
Jan 11, 2002
52
US
Does anyone know how to disable the autoscroll wheel
on the users mouse? I have forms where the user should only
be able to access new records for entry, but the scroll wheel causes record changes, even if I have the cycling of records on "current". Also causes program to crash when
I've set required fields and the wheel tries to jump to another record. Maybe by intercepting the Mouse_event?
 
The only sure way I can think of is to disable the mouse wheel in the mouse set up program in Windows, i.e. set the wheel's function globally to some other function rather than scrolling or disable it completely. This can usually be done via the Mouse option in the Control Panel. The problem with this method is that you would have to do this on all users' computers and ensure that they do not change the settings back. Have fun! :eek:)

Alex Middleton
 
This should do it. Bit long winded, but it does work ver well:

ACC2000: How to Detect and Prevent the Mouse Wheel from Scrolling Through Records in a Form (Q278379)

--------------------------------------------------------------------------------
The information in this article applies to:


Microsoft Access 2000


--------------------------------------------------------------------------------
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).



SUMMARY
Microsoft Access does not provide a method for preventing users from using the mouse wheel to scroll through records on a form. This article shows you how to programmatically prevent users from using the mouse wheel to scroll through records on a form.


MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please see the following page on the World Wide Web:


For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
By default, users can roll the mouse wheel to scroll through a series of records in a Microsoft Access form. If you want to prevent this, you can use the Win32 API to subclass your forms and to ignore mouse wheel messages sent to the form. There are two approaches for doing this. The first approach is to use Microsoft Visual Basic or Microsoft Visual C++ to create an ActiveX DLL that subclasses your Microsoft Access forms, and then to reference that DLL from your Microsoft Access application. A second method is to write all the code within Microsoft Access itself without using an ActiveX DLL. Because of problems with subclassing windows after loading the Microsoft Office Visual Basic Editor, Microsoft highly recommends that you use Microsoft Visual Basic or Microsoft Visual C++ to create an ActiveX DLL, and that you then reference the DLL from your Microsoft Access application.

Creating the MouseWheel Event Completely Within Microsoft Access
WARNING: If possible, you should use the method listed in the "Creating the MouseWheel Event by Using a Visual Basic ActiveX DLL" section earlier in this article. You can use the method discussed in this section only in a situation where the users of your application will not be loading the Visual Basic Editor within Microsoft Access, such as in a Microsoft Access run-time application. If you implement this solution and your users open the Visual Basic Editor, the code in this section will cause Microsoft Access to stop responding. Additionally, you must quit and restart Microsoft Access before testing this code if you have loaded the Visual Basic Editor at least once during the Microsoft Access session. Microsoft highly recommends that you save your work often and that you keep current backups of your database if you use this approach.

The approach demonstrated in this section shows how to use a custom class module to create a custom event named MouseWheel , which you can use in your forms to detect when the user has rolled the mouse wheel. This custom event exposes a Cancel argument that you can use to prevent the mouse wheel roll message from being intercepted by Microsoft Access, thereby preventing record scrolling in the form.

To create the custom procedures, follow these steps:
CAUTION : Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform these steps on a copy of the database.


Start Microsoft Access.


Open the sample database Northwind.mdb.


On the Insert menu, click Module to create a new module in the Visual Basic Editor.


Add the following code to the module:


Option Compare Database
Option Explicit

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long


Public Const GWL_WNDPROC = -4
Public Const WM_MouseWheel = &H20A
Public lpPrevWndProc As Long
Public CMouse As CMouseWheel

Public Function WindowProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

'Look at the message passed to the window. If it is
'a mouse wheel message, call the FireMouseWheel procedure
'in the CMouseWheel class, which in turn raises the MouseWheel
'event. If the Cancel argument in the form event procedure is
'set to False, then we process the message normally, otherwise
'we ignore it. If the message is something other than the mouse
'wheel, then process it normally
Select Case uMsg
Case WM_MouseWheel
CMouse.FireMouseWheel
If CMouse.MouseWheelCancel = False Then
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End If


Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End Select
End Function
On the File menu, click Save . Save the module as basSubClassWindow .


On the Insert menu, click Class Module .


Add the following code to the class module:


Option Compare Database
Option Explicit

Private frm As Access.Form
Private intCancel As Integer
Public Event MouseWheel(Cancel As Integer)

Public Property Set Form(frmIn As Access.Form)
'Define Property procedure for the class which
'allows us to set the Form object we are
'using with it. This property is set from the
'form class module.
Set frm = frmIn
End Property

Public Property Get MouseWheelCancel() As Integer
'Define Property procedure for the class which
'allows us to retrieve whether or not the Form
'event procedure canceled the MouseWheel event.
'This property is retrieved by the WindowProc
'function in the standard basSubClassWindow
'module.

MouseWheelCancel = intCancel
End Property

Public Sub SubClassHookForm()
'Called from the form's OnOpen or OnLoad
'event. This procedure is what "hooks" or
'subclasses the form window. If you hook the
'the form window, you must unhook it when completed
'or Access will crash.

lpPrevWndProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, _
AddressOf WindowProc)
Set CMouse = Me
End Sub

Public Sub SubClassUnHookForm()
'Called from the form's OnClose event.
'This procedure must be called to unhook the
'form window if the SubClassHookForm procedure
'has previously been called. Otherwise, Access will
'crash.

Call SetWindowLong(frm.hwnd, GWL_WNDPROC, lpPrevWndProc)
End Sub

Public Sub FireMouseWheel()

'Called from the WindowProc function in the
'basSubClassWindow module. Used to raise the
'MouseWheel event when the WindowProc function
'intercepts a mouse wheel message.
RaiseEvent MouseWheel(intCancel)
End Sub
On the File menu, click Save . Save the class module as CMouseWheel .


Open the Customers form in Design view.


On the View menu, click Code to view the class module of the form.


Add the following code to the class module of the form:


Option Compare Database
Option Explicit

'Declare a module level variable as the custom class
'and give us access to the class's events
Private WithEvents clsMouseWheel As CMouseWheel

Private Sub Form_Load()
'Create a new instance of the class,
'and set the class's Form property to
'the current form
Set clsMouseWheel = New CMouseWheel
Set clsMouseWheel.Form = Me

'Subclass the current form by calling
'the SubClassHookForm method in the class
clsMouseWheel.SubClassHookForm
End Sub

Private Sub Form_Close()
'Unhook the form by calling the
'SubClassUnhook form method in the
'class, and then destroy the object
'variable

clsMouseWheel.SubClassUnHookForm
Set clsMouseWheel.Form = Nothing
Set clsMouseWheel = Nothing
End Sub

Private Sub clsMouseWheel_MouseWheel(Cancel As Integer)
'This is the event procedure where you can
'decide what to do when the user rolls the mouse.
'If setting Cancel = True, we disable the mouse wheel
'in this form.

MsgBox "You cannot use the mouse wheel to scroll through records."
Cancel = True
End Sub

On the File menu, click Close and Return to Microsoft Access .


Save the form, and then close it.

NOTE: Do not open the form in Form view at this time. If you do, Microsoft Access will stop responding because the Visual Basic Editor has been loaded.


Quit Microsoft Access.


Restart Microsoft Access, and open the sample database Northwind.mdb.


Open the Customers form in Form view.


Roll the mouse wheel.


Note that you receive the message:
You cannot use the mouse wheel to scroll through records.
Also note that the current record has not changed, indicating that the mouse wheel message was not processed by Microsoft Access.
 
Glad to help!

The beauty of this method is that it only affects the one MS Access application - rather than the whole of Windows. Changing users' machine settings is considered rude.

Just watch out for the crash potential of this. Works fine providing you don't open the code window and then try opening a form in "form view" afterwards. Always ensure you close the database and MS Access down and then re-open.

Ed Metcalfe.
 
Was thinking about this scroll wheel issue.

I mainly setup data entry forms, so I think this would work.

If a required field on a form is left blank, the scroll wheel won't goto the next record. It'll give you an error saying that you cant leave a required field blank.

So could we:

Set up a dummy field that requires data entry.
At the end of the form, have a control box that would enter something in that dummy field and then close the form.

That way the records cannot be switched with the scroll wheel. It'll pop up the required data error when tried.

Just a thought,
Jeff
 
I came across a neat solution to this at


This is an Active-X control that you add to each form you want to stop the mousewheel on. Not only does it prevent the mousewheel from scrolling through underlying records, it also allows the wheel to be used to scroll up and down combo drop downs.
 
GREAT POST!!!!!!!!!
i have been crying over the mouse wheel forever! I have ONE user that just didnt grasp that the scroll changes the whole record when in a combo box. He never got it and created HUGE headaches for my autonumber records!
IT WORKS SOOO EASILY!!!!
THANKS A MILLION GARLANJ and A BILLION TO THE WRITER OF THE PAPWALKER MOUSETRAP CONTROL!!!!!!!!!!
 
I personally think McWhorter has the easiest and simplest method here. no code just a nice lttle trick that will work on a shared database across a network. without having to be a programmer to implemenet it

Be ALERT - Your country needs Lerts
 
The code from Microsoft tells me I have a Duplicate Option statement.... is there anything obvious that would cause this error as I have no idea how to search the code for an error!!!

Dawn Coleman, Business Analyst
UnumProvident Corporation

PLEASE RECYCLE AND ENCOURAGE YOUR OFFICE/COWORKERS TO DO SO AS WELL.
 
MorningSun, look for
Option Base
Option Compare
Option Explicit
Option Private


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
A quick note for Access 2003 users ... this version contains a form event which detects the mouse wheel. You can write a 'cheap and cheerful' routine for each form, like this:

Code:
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
    
    If Count > 0 Then
        DoCmd.GoToRecord , , acPrevious
    Else
        DoCmd.GoToRecord , , acNext
    End If
    
End Sub
The parameter Count contains the number of lines to scroll when the mouse wheel is turned by one click. Here, I just look to see if it is a positive number (scroll forwards) or negative (scroll backwards). I then just use a command to reverse the action.

Unfortunately, you can't just use DoCmd.CancelEvent - maybe in the next version of Access ... ?


Bob Stubbs
 
Nice find Bob. Have a star. :)

Ed Metcalfe.

Please do not feed the trolls.....
 
I took a little differant approach to McWhorter's suggestion about using a required field.

In forms that I do not want the mouse wheel to work I provide the following code in the form MouseWheel event.

Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
'This function disables Access cycling records in the form
MsgBox "Mousewheel not allowed in this form."
End Sub

This has the advantage of letting the user know the wheel is not supported in this form.

 
The odd thing about the mouse wheel event in access2003 is that i cant for the life of me think of any other use. That said why didnt the developers just create a yes/no field which disabled or enabled use of the wheel in forms.
Can anyone else think of a use.

"My God! It's full of stars...
 
I hate to be the new guy and say you're all wrong but I've found that none of the above posts work correctly in my setting. I tried the Microsoft code (I've found that the Microsoft code causes a weird loop in the windows procedures that made my program hang)
and I tried the post by Bob, both failed miserably. The problem with the mousewheel on the dataforms is that if you scroll one at a time then you might be able to stop it with those solutions but if the user just scrolls rapidly or pushes the scroll to scroll over multiple records at one time then the code fails on all of these. So here's what I did and it works great ...

1. I created a modular variable in the form called modCancel as Boolean
2. I add this code:
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
modCancel = True
End Sub
3. I add this code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If modCancel = True Then
MsgBox "You cannot use the mousewheel to scroll through records. Use PageUp and PageDown.", vbOKOnly + vbInformation
Cancel = True
modCancel = False
End If
End Sub

What this does is when the user scrolls the mouse wheel then the modular variable is set to true and the form beforeupdate automatically fires and the code in there checks to see if the modular variable is true and if so then send a message and cancel the event. It also resets the modular variable back to false. This cancels the mouse wheel movement completely with no side effects. I have code behind my key press events to make sure that the PageUp and PageDown don't fail also, but I want the user to be able to use those as a scrolling tool.

So give this a try, it's easy, quick and it works for me! Let me know if anyone has a problem that I'm not seeing in my solution.
 
Check out stephen Lebans web site of Access tips. His code is the best I've seen and I have it in one of our large production databases.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top