Link to home
Start Free TrialLog in
Avatar of MonkeyPie
MonkeyPieFlag for Australia

asked on

Access 2010 - tab control within form text box

I have a  large, multi line text box on a form for a NOTES field - bound to a MEMO field.  The user has asked if it is possible to allow TAB key to work within the text box, so he can line up dates and amounts in nice columns - just like he would do in a WORD document.

If he clicks TAB on current form the cursor jumps to the beginning of the text box.

How can I get the TAB key to behave as he has asked?

Thank you.
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

i don't think that is possible with the normal keyboard keys stroke..
If the columns are distinct/important, why not use a subform instead, and allow multiple notes, each with an optional date and amount?

So you would have a separate tblNotes with Notes, DateEntered and Amount  as fields, linked by ID to the parent record.
Avatar of MonkeyPie

ASKER

Thanks for comments.
Miriam - I am stuck with current design.  My dates and amounts was just a simple example of why they might need the tab key.  The NOTES field is used for all sorts of comments, and sometimes they want it to look nicely formatted.

Carpricorn1 - I also don't think it is poossible, but wondered if someone had a nice workaround.

Suzanne
You  *might* be able to use the key down (or keypress) event to test for specific keys such as tab (or some other key) and append a tab  (chr(9)) to the end of the data in the text box.
Yes - might try that.  Or I could add some fixed number of space characters to simulate a tab?
I added the following code to the keypress event:

Private Sub txtNotes_KeyPress(KeyAscii As Integer)
    If KeyAscii = 27 Then Call cmdCancel_Click
    If KeyAscii = 9 Then
        KeyAscii = 32       ' 32= space
        txtNotes.Text = txtNotes.Text & " **TAB** "
    End If
End Sub

Open in new window

I used **TAB** just for testing purposes.
In the text box, if I type
test followed by the tab character, I get
<space>test**TAB**
but the cursor has jumped to the beginning of the string - where it puts the space char.

So, the TAB character is not being 'overridden' by my if ascii=9 then ascii=32 line.

Close!

But I need to cancel the TAB character after I replace it with **TAB** string, or whatver I eventually replace it with.

Any ideas?
Here you go.  This is a little rough.... but work with this a little bit.

In your forms code DECLARATION SECTION:

Dim vback As Boolean

Now, in the memo box - in the KEY DOWN event:

Dim s As String
s = Me.YourMemoBox.Text
If KeyCode = vbKeyTab Then
    s = s & "     "
    Me.YourMemoBox = s
    vback = True
End If


Now, in the next control that gets the focus (next tab order):

In the ON ENTER event:

If vback = True Then
    vback = False
    Me.YourMemoBox.SetFocus
End If


You will have to play with the "selstart" stuff to get your cursor to the end of the text....  but hey, it works.

Scott C
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually you CAN get a true tab in a textbox... not just a approximation using spaces (which is not quite the same thing), but you need one of those numeric keypads on the side of your keboard, or an attached numeric keypad.

- In your table's design set the "Text Format" property of your memo field to Rich Text
- In your form's design, set the Text Format property of your textbox to Rich Text (suprisingly, this is under the Data tab in the property sheet, not the Format tab)
- For a Tab character use Alt-9, using the 9 on the numeric keypad - not the 9 above the letters.  

(The tab appears once you release the Alt key.)
Miriam,
That is amazing!  But sadly, I can't change the design of the underlying table - it is used by others.

How on earth did you figure this out?
Suzanne
Almost blind luck.   I knew the Alt key could be used to enter ascii codes, and had been trying it with my laptop keyboard for this question to no avail.  

My son, coincidentally just showed me today some cool things HE learned how to do with the numeric keypad on the larger keyboard he has attached to his computer... Which automatically made me think of this issue again.

So I've got him to thank for that little trick. :-)

Curious whether it would work with the Text Format setting in the text box alone, without the accompanying setting in the table?  I probably won't have a chance to check that myself before Monday.
And on a bit of a tangent...  Really showing my age now... That is actually how we created tables, etc in documents in the first word processor I owned.

Alt + some ascii code to insert each segment of double line or corner pieces for outside frames, and underscores, dashes or equals signs for each segment of horizontal line.  Fun, but tedious.
I tried the 'rich text' idea on the form text box alone, without changing the bound field and it did not work.  I got a message saying I need to change the underlying field type.

Thank you anyway.  For now I will use the code you posted previously, although it only inserts spaces, so there is still an issue about lining things up nicely.  But better than the native action of TAB which puts cursor back to the beginning.