Mouse scroll wheel clears input form data

Posted on 2005-04-05
Last Modified: 2008-01-09
I have an input only form with combo boxes, text boxes, and so on. The form is built so it doesn't save any data until the user clicks the save button.

The problem that has popped up is if a user uses the mouse scroll wheel during the input process even when focus is in a text or memo box it clears the entered form data and they lose all the input to that point.

Is there a simple way to catch and stop this from happening? I have looked at posts to stop the mouse wheel from moving through recordsets but I don't know if this is the right code for what I need also.

I need input from your great minds on this one, thanks
Question by:AZweb
    LVL 7

    Accepted Solution


    This is  a code I got from the Microsoft website and it has been working fine with my projects.

    Create a CLASS module (tools > Macro > Visual Basic Editor > right-click on Modules (look for Project Explorer, then in the Project Explorer window select Modules) > INSERT > Class Module

    1) In the new class module paste:
    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
    '========================================= End class module

    2)Save class module. Name it :


    3)In the form you want to disable the mouse wheel paste this in the declarations section (very top under compare database):

    Option Explicit
    Private WithEvents clsMouseWheel As CMouseWheel

    4)In the form On Open argument paste
        Set clsMouseWheel = New CMouseWheel
        Set clsMouseWheel.Form = Me

    5) Anywhere in the code of the form paste:

    Private Sub clsMouseWheel_MouseWheel(Cancel As Integer)
         MsgBox "You cannot use the mouse wheel to scroll through records.", , "Your Title"
         Cancel = True
    End Sub

    6) In the On Close of the form paste:

        Set clsMouseWheel.Form = Nothing
        Set clsMouseWheel = Nothing

    This should do it.

    LVL 11

    Expert Comment

    Here's the QB article link:
    LVL 7

    Expert Comment

    by:Jonathan Kelly
    have you tried setting the cycle property of the form to 'Current Record' ?
    LVL 4

    Author Comment

    setting cycle property to 'Current Record' diddn't help but using 'Current Page' helped but is not the final answer.
    LVL 7

    Expert Comment

    Current Page will not help if you need to use contiguous forms. At least it did not work for me
    LVL 7

    Expert Comment

    by:Jonathan Kelly
    maybe a mouse without a mouse wheel

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    In the previous article, Using a Critera Form to Filter Records (, the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
    The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now