Here’s the code I used to trap the Tab key and use it to generate a”real” tab in a text box:
' Trap the tab key to allow tabs in a text box ' This function should be called by the lostFocus ' event for the control that needs to snag the tab ' Parameters: ' txtControl text box control ' Setting keyboard info Declare Function GetKeyState% Lib "User" _ (ByVal nVirtKey%) ' Virtual key values Global Const VK_TAB = &H9 Sub snagTab (txtControl As Control) Dim retVal As Integer, currSelStart As Long retVal = GetKeyState(VK_TAB) If retVal = -128 Or retVal = -127 Then ' tab key pressed currSelStart = txtControl.SelStart If currSelStart = 0 Then txtControl.Text = Chr$(9) & txtControl.Text Else txtControl.Text = Left(txtControl.Text, _ currSelStart) & Chr$(9) & _ Mid(txtControl.Text, currSelStart + 1) End If' Change the focus back to this control and ' reset the current insert point to past ' the new "tab" txtControl.SetFocus txtControl.SelStart = currSelStart + 1 End If End Sub