MS Access textbox mouse wheel scroll using VBA

Mouse wheel scrolling on Textbox.

Microsoft provide me scroll bar option for enable scrolling on textbox while it open in form view. We can set horizontally and vertically whenever it required. But whenever we try to scrolling using mouse wheel, nothing would happen. Is Microsoft doesn't provide any function for enable mouse wheel? Yes, it's true. Mouse wheel scrolling on textbox in form view is not optional while using Microsoft Access. Microsoft doesn't have built in functionality for mouse wheel scrolling on textbox. In form view of Microsoft Office Access, when you try to use the mouse wheel to scroll through records in a form, nothing happens. This article is about to enabled the mouse wheel, page up and page down, line up and line down.

But this is not a problem at all. We can enable mouse wheel scrolling using VBA code. In this article we are creating a function that will enable the mouse wheel scrolling on textbox. To implement this we are creating a module with some APIs and then adding an event on the mouse wheel to the form containing the text box to allow the mouse wheel moves on. We are calling the API function SendMessage(), where the control is assigned, the type of message is assigned, and the type of scroll is assigned. For this we have to create form with textbox field and bound it with table field which contain the text as shown in Fig 1.1. And on the form open and on mouse wheel event we have to write the code.

Enable mouse wheel scroll of textbox using VBA. Fig-1.1

Fig-1.1

After implementing the code we have to test this code. For that open the form in form view. Firstly it will show the message of enabled control and after click on that we can use these control on textbox as shown in Fig 1.2.

Enable mouse wheel scroll of textbox using VBA. Fig-1.2

Fig-1.2

VBA code for declarative constants and function for send message API:

Option Compare Database
Option Compare Database
Option Explicit
Private Declare PtrSafe Function apiGetFocus Lib "user32" Alias "GetFocus" () As Long
Private Const sModName = "Module1_scroll"
Public Const WM_VSCROLL = &H115
Public Const WM_HSCROLL = &H114
Public Const SB_PAGEUP = 2
Public Const SB_PAGEDOWN = 3
Public Const SB_LINEUP = 0
Public Const SB_LINEDOWN = 1
Public Declare PtrSafe Function trans_msg Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Function Fun_handler(CNTL As control) As Long
CNTL.SetFocus
Fun_handler = apiGetFocus
End Function

VBA code for form open and on mouse wheel event:

Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
MsgBox ("Enabled Control: Mouse Wheel, Page up and Page down, Line up and Line Down")
Me.txt_text.SetFocus
End Sub
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal nline As Long)
Dim Hcntrl As Long
Dim n As Long
If ActiveControl.Properties.Item("controltype") = acTextBox Then
Hcntrl = Fun_handler(Screen.ActiveControl)
For n = 1 To Abs(nline)
trans_msg Hcntrl, WM_VSCROLL, IIf(nline < 0, SB_LINEUP, SB_LINEDOWN), 0&
Next
End If
End Sub


DISCLAIMER

It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. Access Guru will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.