ACC97: How to Prevent Use of the Mouse Wheel to Scroll Through Records in a Form (308636)



The information in this article applies to:

  • Microsoft Access 97

This article was previously published under Q308636
Advanced: Requires expert coding, interoperability, and multiuser skills.

SUMMARY

This article describes how to create an ActiveX DLL to 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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. By default, users can roll the mouse wheel to scroll through a series of records in an Access form. Microsoft does not provide a way to prevent this action, but you can prevent this action by using the Win32 API to subclass the forms and to cause Access to ignore mouse-wheel input to the form.

Although you can write all of the necessary code within Access itself without using an ActiveX DLL, Microsoft strongly recommends that you use Microsoft Visual Basic or Microsoft Visual C++ to create an ActiveX DLL that subclasses Access forms, and then create a reference to that DLL.

Creating an ActiveX DLL in Visual Basic

  1. Start Visual Basic 6.0.
  2. Create a new ActiveX DLL project, and then open the project.
  3. Add the following code to the class module window that appears:
    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
    					
  4. Set the class module properties as follows:
       Class Module: CMouseWheel
       -------------------------
       Name: CMouseWheel
       Instancing: 5 - MultiUse
    					
  5. Add a standard module to the project, and then add the following 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
    					
  6. On the View menu, click Project Explorer to view the Project Explorer.
  7. Click the project node at the very top of Project Explorer.
  8. On the View menu, click Properties to view the property sheet for the project.
  9. Set the project's Name property to MouseWheel.
  10. On the File menu, click Save Project.
  11. Save the project files as basSubClassWindow.bas, CMouseWheel.cls, and MouseWheel.vbp respectively.
  12. On the File menu, click Make MouseWheel.dll, and then click OK to make the DLL.
  13. Quit Microsoft Visual Basic.

Creating a Reference to the ActiveX DLL

  1. Start Microsoft Access, and then open the sample database Northwind.mdb.
  2. Open the Customers form in Design view.
  3. On the View menu, click Code to display the module of the form in the Visual Basic Editor.
  4. On the Tools menu, click References.
  5. To select the reference, click to select the check box next to MouseWheel. If this reference is not listed, click Browse, click MouseWheel.dll in the folder where you saved it (in Step 12 of the preceding procedure), and then click Open.
  6. Click OK to close the References dialog box.
  7. Add the following code to the module of the form:
    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 "You cannot use the mouse wheel to scroll records."
        Cancel = True
    End Sub
    					
  8. On the File menu, click Close and Return to Microsoft Access.
  9. Save and close the Customers form.
  10. To view the results, open the Customers form in Form view, and try to scroll by using the mouse wheel. Note that you receive the following message:
    You cannot use the mouse wheel to scroll records.
    Also note that the current record has not changed, which indicates that Access did not process the rolling of the mouse wheel.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbhowto kbProgramming KB308636