Microsoft KB Archive/308636

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

Article ID: 308636

Article Last Modified on 1/31/2007

-

APPLIES TO


 * Microsoft Access 97 Standard Edition

-



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
 Start Visual Basic 6.0. Create a new ActiveX DLL project, and then open the project.  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   Set the class module properties as follows:   Class Module: CMouseWheel -  Name: CMouseWheel Instancing: 5 - MultiUse   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 &quot;user32&quot; Alias &quot;SetWindowLongA&quot; _ (ByVal hwnd As Long, _   ByVal nIndex As Long, _    ByVal dwNewLong As Long) As Long

Public Declare Function CallWindowProc Lib &quot;user32&quot; Alias &quot;CallWindowProcA&quot; _ (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  On the View menu, click Project Explorer to view the Project Explorer.</li> Click the project node at the very top of Project Explorer.</li> On the View menu, click Properties to view the property sheet for the project.</li> Set the project's Name property to MouseWheel .</li> On the File menu, click Save Project.</li> Save the project files as basSubClassWindow.bas, CMouseWheel.cls, and MouseWheel.vbp respectively.</li> On the File menu, click Make MouseWheel.dll, and then click OK to make the DLL.</li> Quit Microsoft Visual Basic.</li></ol>

Creating a Reference to the ActiveX DLL
<ol> Start Microsoft Access, and then open the sample database Northwind.mdb.</li> Open the Customers form in Design view.</li> On the View menu, click Code to display the module of the form in the Visual Basic Editor.</li> On the Tools menu, click References.</li> 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.</li> Click OK to close the References dialog box.</li>  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 &quot;You cannot use the mouse wheel to scroll records.&quot; Cancel = True End Sub </li> On the File menu, click Close and Return to Microsoft Access.</li> <li>Save and close the Customers form.</li> <li>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.</li></ol>

Additional query words: inf

Keywords: kbhowto kbprogramming KB308636

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.