Solved

Access 2010 - tab control within form text box

Posted on 2013-11-27
13
1,757 Views
Last Modified: 2013-11-29
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.
0
Comment
Question by:MonkeyPie
13 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39682374
i don't think that is possible with the normal keyboard keys stroke..
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39682494
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.
0
 

Author Comment

by:MonkeyPie
ID: 39682569
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
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39682671
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.
0
 

Author Comment

by:MonkeyPie
ID: 39682676
Yes - might try that.  Or I could add some fixed number of space characters to simulate a tab?
0
 

Author Comment

by:MonkeyPie
ID: 39682699
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?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Expert Comment

by:clarkscott
ID: 39685016
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
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39685513
Sorry for the delay - Thanksgiving.

Your own code is actually pretty close and this is definitely doable in a single event, without letting the focus leave the textbox and forcing it back.

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 & "         _"
        
        Me.txtNotes.SelStart = Len(Me.txtNotes.Text) - 1
        Me.txtNotes.SelLength = 1
        
    End If
End Sub

Open in new window


The code uses spaces instead of an actual tab, and places an underline at the end so that the position is recognized (trailing spaces or a tab alone are not quite adequate).  The last underline is selected, so it is overwritten by any additional characters typed (but you will have a trailing underline if nothing is typed following a tab.  (You could take care of that with code in the textbox's after update event to remove the underscore if it appears as the last character.)
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39686306
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.)
0
 

Author Comment

by:MonkeyPie
ID: 39686314
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
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39686380
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.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39686388
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.
0
 

Author Closing Comment

by:MonkeyPie
ID: 39686391
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now