PDA

View Full Version : Disable the Mouse Wheel



CFDM
12-17-2006, 10:15 AM
Is there any possible way to disable the God awful mouse wheel. Somehow it is bypassing all of the validation I have. The validation works on nextrec, and on exit- :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :bug: :motz2: HELP!!!!!!!

WHICH EVENT IS SURE TO CATCH VALIDATION???????

Prasad_Joshi
12-18-2006, 02:58 AM
Take reference of this & try.


' Class module for ActiveX dll 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 ' Standard Module CodeOption 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 ' Subs coded: Form LOAD Code/ Form CLOSE code/ clsMouseWheel_MouseWheel code ' Make sure you have referenced the MouseWheel.dll before you insert this code. Also ' Ensure that you are not going to overwriite an existing code if you are cutting and ' pasting. Either copy before or after you existing code. 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 "The Mouse Wheel has been disabled. Record shouldn't advance." Cancel = True End Sub
How to use:


This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).

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


MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please see the following page on the World Wide Web:
http://support.microsoft.com/default.aspx?scid=http://www.microsoft.com/partner/refer ral/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com/ directory/overview.asp
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 by Using a Visual Basic ActiveX DLL
Start Microsoft Visual Basic 6.0.


Create a new ActiveX DLL project, and then open it.

Add the code labeled Class Module for ActiveX dll to the class module window that appears.
In the properties window of the Class Module set the properties as follows:
Name: CMouseWheel
Instancing: 5-MultiUse

Then Add a standard module to the project, and then insert the standard module code
Once this code has been added to the new module. Click the project obnject to view the project properties. Set the project's name property to MouseWheel. Then Save the project. Save the project files as BasSubClassWindow.bas, CMouseWheel.cls, and MouseWheel.vpb. Once all modules and project is saved. From the File menu, click Make MouseWheel.dll. When you click OK it will create the DLL.


Exit VB.6
Start the Access appllication that you wish to disable the mouse.

Open the form you want to disable in Design mode or simply select the form and click Code View from the top toolbar. In the Tools Menu select Reference. Find the MouseWheel.dll or browse to the folder you saved the dll file. Then click Open.

Then you need to add the last segment of code into the form code. Be sure you do not overwirte any pre-existing code in the Close and Load Subroutines. When all code is added Save the Form and close the Code view. Open the form in Form view and note the Message when you attempt to scroll.
Test the code:


The code is already inserted in the Orders form. I have commented them outl so that the reference can be made before the code is run. After the reference is declared simply uncomment the
'Private With clsMousewheel....
and the next three subroutines: Load, CLOSE, and clsMouseWheel_MouseWheel

When you load the Orders Form which contains the updated LOAD, ClOSE, and clsMouseWheel_MouseWheel subs. Simply scroll your mouse wheel. If your record changed there is an error in your code. If it doesn't advance or iterate through the records it worked.

JimmyTheHand
12-18-2006, 05:03 AM
A possible workaround:

When you use mousewheel to change the current record, the form's Current event launches. Maybe, combined with MouseWheel event, you can use it to validation purposes. My idea is something like this:

Public WheelDirection As Long
Public EventIsProcessed As Boolean

Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
WheelDirection = Sgn(Count)
End Sub WheelDirection will hold the direction the wheel has been turned in. This event launches before Form_Current.

Form_Current event launches when the record has already been updated. In order to validate the previously displayed record, we must step back to it, using the (known) direction of the wheel's turning.
Private Sub Form_Current()
If EventIsProcessed Then Exit Sub
EventIsProcessed = True


Select Case WheelDirection
Case 1 'it was a forward step through records
DoCmd.GoToRecord , , acPrevious 'so now let's step one record back
Case -1 'it was a backward step through records
DoCmd.GoToRecord , , acNext 'so now let's step one record forward
End Select
'...
'Here comes the code to validate the record.
'...
If RecordIsValid Then
Select Case WheelDirection
Case 1 'the user wanted to step forward,
DoCmd.GoToRecord , , acNext 'so let's step forward
Case -1 'the user wanted to step back,
DoCmd.GoToRecord , , acPrevious 'so let's step back
End Select
Else
MsgBox ("Not valid")
End If
EventIsProcessed = False
End Sub

The variable EventIsProcessed is used to disable Current event while we do steps back and for through records, so that it doesn't go to an endless loop. It's like Application.EnableEvents = False in Excel, but I didn't find it's equivalent in Access. There may be a better solution for this.

Note: I didn't test this code in any way, it's just an idea. But it might work.

Carl A
12-18-2006, 09:17 AM
I downloaded this and it seems to work. It disabled the scrolling of records on the test form provided.

http://www.lebans.com/mousewheelonoff.htm

andrew93
12-26-2006, 11:56 PM
Hi Carl

You can download a mousewheel.dll file from my website here:
http://www.accessdata.co.nz/Downloads/MouseWheel.dll

and use it like this:

1. Open your form in Design view.
2. Click menu option View > Code to open the Visual Basic Editor.
3. Click menu option Tools > References > Browse > find and click MouseWheel.dll > Open.
4. Click OK to close the References dialog box.
5. 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
6. Save and close the Visual Basic Editor. Save and close the form. Re-open the form and you won't be able to use the scroll wheel on that form.

HTH, Andrew

Late Edit : I see there is a KB article here : http://vbaexpress.com/kb/getarticle.php?kb_id=890