***NEW CUSTOMER OFFER***
Get 15 minutes of free Live Help!

Scroll in a Microsoft Access text box using the mouse wheel

4 comments

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 () event procedure.

Trying to 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 bar then the text box bar. I was surprised to learn that the mouse wheel that I use for everything does not work by default in a text box.

There are three steps to providing that ability:

  1. Add some constants and an API function to the declarations section of a code module.
  2. Include a function in the code module that will get a handle for the text box control.
  3. 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 (), 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 () 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 .
' You are free to use it in any ,
' 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..Item("") = 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

Download:

basMouseWheel.zip

  1. This works very well thanks!

  2. Hi, I am (very) new to VBA Access and tried your code on a single textbox in a form. This works very well, thanks!

    However, when I have a textbox within a navigation control, the code doesn’t work. Can you please help me out?

    (sorry for my poor English….)

  3. I searched for this solution for quite a while, once I figured out where to put everything this works perfectly.

    Great job! Just wish google had found you quicker 🙂

  4. Tobias Gårdner says:

    Hi! Tried this on my Access 2016 64-bit but it does not work. Is there an update available or information on how to patch it to work with 64-bit Office?

Leave a Reply

Your email address will not be published. Required fields are marked *

Live Chat Software
%d bloggers like this: