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.
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 "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
On the View menu, click Project Explorer to view the Project Explorer.
Click the project node at the very top of Project Explorer.
On the View menu, click Properties to view the property sheet for the project.
Set the project's Name property to MouseWheel.
On the File menu, click Save Project.
Save the project files as basSubClassWindow.bas, CMouseWheel.cls, and MouseWheel.vbp respectively.
On the File menu, click Make MouseWheel.dll, and then click OK to make the DLL.
Start Microsoft Access, and then open the sample database Northwind.mdb.
Open the Customers form in Design view.
On the View menu, click Code to display the module of the form in the Visual Basic Editor.
On the Tools menu, click References.
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.
Click OK to close the References dialog box.
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
On the File menu, click Close and Return to Microsoft Access.
Save and close the Customers form.
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.
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.