Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005

    Unanswered: Prevent mouse wheel from scrolling through records

    Hello to all,

    I've been searching all over trying to find out how to prevent the mouse from scrolling through the records in my access database withough having to load a dll on every machine. I've finally found the answer. I did not write this and the poster's user name is Ed2020 on the page that I found the solution so all credit goes to this user and how ever he/she got the answer. I just though others could use this information seeing as this is my main source for information i wanted to share what I've found.

    ************************************************** ********

    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).

    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.

    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
    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

    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

    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
    End Sub

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

    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.

    ************************************************** ********

    I hope this is beneficial to all.


  2. #2
    Join Date
    Dec 2004
    Madison, WI

    Mouse scrolling

    If you search on this forum for Mouse Scroll, there's a lot of other posts on how to do this and I think you'll find a few posts which don't have a ton of code to disable it. I used one I found which only had a few lines of code in it - no dll (not to burst your bubble) but I'll give yours a test - thanks for the info.
    Last edited by pkstormy; 08-07-06 at 15:43.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  3. #3
    Join Date
    Jul 2012

    Record Scrolling

    Mythos, your solution took me less than 10mins to implement and has prevented an issue the Team have had accidentally scrolling when adding a new record. Clear and helpful instructions. Brilliant! thanks

  4. #4
    Join Date
    Jun 2005
    Richmond, Virginia USA
    Provided Answers: 19
    I'm sorry, but this section of the forum is for seeking help with problems/questions, not for posting answers to unasked questions! If you feel the need to share this type of information, with members, the proper place would be in the Code Bank thread, which is 'stuck' at the beginning of this section, or at

    And even more to the point, why would you use up bandwidth, here, copying and pasting, word for word, an entire MS Knowledge Base Article (which is what this is) instead of simply giving the URL for the article?

    Linq ;0)>
    Last edited by Missinglinq; 07-23-12 at 19:38.
    Hope this helps!

    The problem with making anything that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Join Date
    Jul 2012
    from a first time user... <expression deleted>
    Last edited by healdem; 07-24-12 at 10:36.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts