Microsoft Access does not have built-in mouse wheel scrolling in form text boxes. You can provide mouse wheel scrolling by writing some VBA code in the Form_MouseWheel() event procedure.
Trying to scroll in a text box can be particularly frustrating if the bottom of the text box is below the bottom of the screen and you wish to navigate downward without having to alternately use the form scroll bar then the text box scroll bar. I was surprised to learn that the mouse scroll wheel that I use for everything does not work by default in a text box.
There are three steps to providing that ability:
- Add some constants and an API function to the declarations section of a code module.
- Include a function in the code module that will get a handle for the text box control.
- Write a procedure for the On Mouse Wheel event of the form containing the text box.
The key to the solution is the call to the API function SendMessage(), where the control is assigned, the type of message is assigned, and the type of scroll is assigned.
(1) Declarations section:
' Mouse wheel-related constants. Public Const WM_VSCROLL = &H115 Public Const WM_HSCROLL = &H114 Public Const SB_LINEUP = 0 Public Const SB_LINEDOWN = 1 ' Used in mouse wheel code. Public Declare Function SendMessage Lib "User32" _ Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Any) As Long ' Used in fhWnd() to get a handle on the text box used in mouse wheel code. Public Declare Function apiGetFocus Lib "User32" _ Alias "GetFocus" _ () As Long
(2) Control handle function:
Public Function fhWnd(ctl As Control) As Long ' fhWnd() was originally written by Dev Ashish. ' It is not to be altered or distributed, ' except as part of an application. ' You are free to use it in any application, ' provided the copyright notice is left unchanged. ' ' Code Courtesy of ' Dev Ashish On Error Resume Next ctl.SetFocus If Err Then fhWnd = 0 Else fhWnd = apiGetFocus End If On Error GoTo 0 End Function
(3) Form code:
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long) ' This procedure runs when the mouse wheel is used. ' Copyright © 2014 Extra Mile Data, www.extramiledata.com. ' For questions or issues, please contact [email protected]. ' Use (at your own risk) and modify freely as long as proper credit is given. ' Proper credit includes mentioning the example by eggieman at ' http://www.access-programmers.co.uk/forums/showthread.php?t=195679 ' and the fhWnd() function by Dev Ashish at ' http://access.mvps.org/access/api/api0027.htm. On Error GoTo Err_Handler Dim intLinesToScroll As Integer Dim hwndActiveControl As Long ' Let the mouse wheel scroll in text boxes. If ActiveControl.Properties.Item("ControlType") = 109 Then hwndActiveControl = fhWnd(Screen.ActiveControl) If Count < 0 Then For intLinesToScroll = 1 To -1 * Count SendMessage hwndActiveControl, WM_VSCROLL, SB_LINEUP, 0 Next Else For intLinesToScroll = 1 To Count SendMessage hwndActiveControl, WM_VSCROLL, SB_LINEDOWN, 0 Next End If End If Exit_Proc: On Error Resume Next Exit Sub Err_Handler: MsgBox Err.Number & " " & Err.Description, vbCritical, _ "Form_MouseWheel()" Resume Exit_Proc End Sub