Access

Disabling Mouse Scroll wheel from advancing database entries

Ease of Use

Easy

Version tested with

2003 

Submitted by:

Imdabaum

Description:

This code enables you to keep users from scrolling through records in Access by disabling the scroll button/mouse wheel 

Discussion:

Although I have been told the mouse event is actually included in Access 2003, I wasn't able to find any references to it. So this is an easy fix. 

Code:

instructions for use

			

' Class module for ActiveX dll Option Compare Text Option Explicit Private frm As Object Private intCancel As Integer Public Event MouseWheel(Cancel As Integer) Public Property Set Form(frmIn As Object) Set frm = frmIn End Property Public Property Get MouseWheelCancel() As Integer MouseWheelCancel = intCancel End Property Public Sub SubClassHookForm() lpPrevWndProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, _ AddressOf WindowProc) Set CMouse = Me End Sub Public Sub SubClassUnHookForm() Call SetWindowLong(frm.hwnd, GWL_WNDPROC, lpPrevWndProc) End Sub Public Sub FireMouseWheel() RaiseEvent MouseWheel(intCancel) End Sub ' Standard Module Code Option Compare Text Option Explicit Public CMouse As CMouseWheel 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 Function WindowProc(ByVal hwnd As Long, _ ByVal uMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long 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 ' Subs coded: Form LOAD Code/ Form CLOSE code/ clsMouseWheel_MouseWheel code ' Make sure you have referenced the MouseWheel.dll before you insert this code. Also ' Ensure that you are not going to overwriite an existing code if you are cutting and ' pasting. Either copy before or after you existing code. Option Compare Database Option Explicit Private WithEvents clsMouseWheel As MouseWheel.CMouseWheel Private Sub Form_Load() Set clsMouseWheel = New MouseWheel.CMouseWheel Set clsMouseWheel.Form = Me clsMouseWheel.SubClassHookForm End Sub Private Sub Form_Close() clsMouseWheel.SubClassUnHookForm Set clsMouseWheel.Form = Nothing Set clsMouseWheel = Nothing End Sub Private Sub clsMouseWheel_MouseWheel(Cancel As Integer) MsgBox "The Mouse Wheel has been disabled. Record shouldn't advance." Cancel = True End Sub

How to use:

  1. This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).
  2. SUMMARY
  3. 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.
  4. MORE INFORMATION
  5. 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:
  6. http://support.microsoft.com/default.aspx?scid=http://www.microsoft.com/partner/refer ral/
  7. For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com/ directory/overview.asp
  8. 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.
  9. Creating the MouseWheel Event by Using a Visual Basic ActiveX DLL
  10. Start Microsoft Visual Basic 6.0.
  11. Create a new ActiveX DLL project, and then open it.
  12. Add the code labeled Class Module for ActiveX dll to the class module window that appears.
  13. In the properties window of the Class Module set the properties as follows:
  14. Name: CMouseWheel
  15. Instancing: 5-MultiUse
  16. Then Add a standard module to the project, and then insert the standard module code
  17. Once this code has been added to the new module. Click the project obnject to view the project properties. Set the project's name property to MouseWheel. Then Save the project. Save the project files as BasSubClassWindow.bas, CMouseWheel.cls, and MouseWheel.vpb. Once all modules and project is saved. From the File menu, click Make MouseWheel.dll. When you click OK it will create the DLL.
  18. Exit VB.6
  19. Start the Access appllication that you wish to disable the mouse.
  20. Open the form you want to disable in Design mode or simply select the form and click Code View from the top toolbar. In the Tools Menu select Reference. Find the MouseWheel.dll or browse to the folder you saved the dll file. Then click Open.
  21. Then you need to add the last segment of code into the form code. Be sure you do not overwirte any pre-existing code in the Close and Load Subroutines. When all code is added Save the Form and close the Code view. Open the form in Form view and note the Message when you attempt to scroll.
 

Test the code:

  1. The code is already inserted in the Orders form. I have commented them outl so that the reference can be made before the code is run. After the reference is declared simply uncomment the
  2. 'Private With clsMousewheel....
  3. and the next three subroutines: Load, CLOSE, and clsMouseWheel_MouseWheel
  4. When you load the Orders Form which contains the updated LOAD, ClOSE, and clsMouseWheel_MouseWheel subs. Simply scroll your mouse wheel. If your record changed there is an error in your code. If it doesn't advance or iterate through the records it worked.
 

Sample File:

NorthWind.zip 571.29KB 

Approved by mdmackillop


This entry has been viewed 154 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express