Using TAB in Memo Field

G

Guest

While editing a Memo Field, I know I can use onKeyPress to test the input,
and test for the TAB character. If True, how can I auto-insert (4) spaces at
the cursor location within the memo field?
 
A

Allen Browne

A sequence like this should work:

1. Use SelStart and SelLength to identify where the cursor is, and how many
characters are selected. (Any selected characters will be overwritten.)

2. Use Left() on the Text property of the control to get the characters
before the cursor.

3. Use Mid() to get the remaining characters after the end of SelStart +
SelLength.

4. Destroy the keystroke (by settting KeyAscii to zero.)

5. Assign the Left() part & the 4 space & the remaining part to the Value of
the Memo.

6. Set SelStart to the previous value of SelStart + 4.
 
G

Guest

Hi Allen,

On reading your suggestion, I realized I should be using the OnKeyDown event.

The first Sub below is my incarnation, the second I found on the Web. They
both suffer from the same 'anamoly'. Both will insert spaces for a TAB press
anywhere in the memo field. As a test, I deleted spaces made by Tabbing,
then hit TAB again -- strangely, the new TAB worked, but then all the
previous Tabs were restored as well! Spooky! ? ! ?

Private Sub txtHelp_KeyDown(KeyCode As Integer, Shift As Integer)

Me!CkDirty = True

If KeyCode = vbKeyTab Then
KeyCode = 0
txtHelp = Left(txtHelp.Value, txtHelp.SelStart) & " " &
Mid(txtHelp.Value, txtHelp.SelStart + 1)
txtHelp.SelStart = txtHelp.SelStart + 4

End If
End Sub

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
Dim stFront As String
Dim stBack As String
Dim lPos As Long
If KeyCode = 9 and Shift = 0 Then
KeyCode = 0
lPos = Text0.SelStart
stFront = Left(Text0.Value, lPos)
stBack = Mid(Text0.Value, lPos + 1)
Text0.Value = stFront & Space(5) & IIf(Trim(stFront) = "", ".", "")
& stBack
Text0.SelStart = lPos + 5
If Trim(stFront) = "" then SendKeys "{del}"
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top