PDA

View Full Version : converting a carriage return (line feed) to a tab



awsmitty
11-29-2008, 11:19 AM
I am very new, to both VBA and developing forms, just started 2 days ago. I would like to create a text box so that if the user were to hit return, the cursor would advance as if the tab key were hit. In other words the return key would not allow a line feed.

I don't know if this is possible, but after purusing the internet, I am not the only one who has confronted this problem, but I can not find a solution. I hate saying that because I am certain it is right here in this forum, but I can't find the answer.

Is what I'm tring to do possible?

awsmitty

Oorang
11-29-2008, 02:20 PM
It should do that by default. Highlight the control and press F4 to go to the control's properties. Make sure Multiline and EnterKeyBehavior are both set to False.

macropod
11-29-2008, 07:15 PM
Hi awsmitty,

Contrary to Oorang's assurance, that is not the the default behaviour - <CR> is not the same as <TAB>.

Before we can help you, we need to know the context - is the 'textbox' you're describing a standard textbox in an ordinary document, a text formfield, or a field on a userform? If it's the first of these, I don't think there's a way to trap this behaviour.

Cosmo
12-01-2008, 07:43 AM
I have used the following to map the left and right arrow keys to tab and shift tab, you should be able to adjust it for your needs:

Private Sub mTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
' Remap left and right arrow keys to Tab and Shift Tab to allow navigation left and right.
If KeyCode = 39 Then
KeyCode = 9
ElseIf KeyCode = 37 Then
SendKeys "+{TAB}"
End If
End Sub

Oorang
12-01-2008, 08:58 AM
Well... Contrary to Macropod's contradiction ;) The enter key behavior of a text box in a user form on Word 2003 is set by a text box propert called (wait for it) "EnterKeyBehavior" :) This property when True will cause the enter key to create a linebreak within the textbox. When the EnterKeyBehavior property is False it will cause the next control to be selected as if tab was pressed. Want to guess what the default is?

(Hint: It's False)

:biggrin:

fumei
12-01-2008, 11:46 AM
"Well... Contrary to Macropod's contradiction The enter key behavior of a text box in a user form on Word 2003 is set by a text box propert called (wait for it) "EnterKeyBehavior"

This may be true...however, as macropod mentions, we do NOT know the OP is even talking about a userform. Assuming that it is a userform, is just that...an assumption.

It is that darn word "form". To many people, "form" is a word document with user-entry points..such as formfields. There are many threads here where "form" is used to refer to a document, not a userform. At this point we do not know.

Oorang
12-01-2008, 12:20 PM
Hi fumei, long time no see:) You have a good point... Smitty, were you talking about a user form or a control embedded in the word doc?

fumei
12-01-2008, 12:39 PM
Which...ahem...is exactly what macropod already asked.

<grin>I am just razzin' ya.</grin> No criticism intended.

Until we really, and for sure, know what the OP is working with - and personally I suspect it is a formfield - we are kind of stuck.

However, macropod also makes a very good point (although I doubt the OP is using one). If it is a "textbox" - not a text formfield in the document, OR a textbox on a userform.... - then it is essentially impossible to trap the Enter key on a real-time basis.

Oorang
12-01-2008, 03:21 PM
:) Yah but I thought his question bore repeating :)

fumei
12-02-2008, 12:37 AM
I believe three of us have asked it now....



and so it goes.

dorian
05-28-2009, 08:42 PM
While the original submitter seems to have abandoned this thread, I would like to hi-jack it because I have a similar question.

I am using Word 2003 and I have a plain old Text Formfield (not a userform, combobox, etc..).

Actually, I have several text formfields contained in various table cells. I can "limit" user from seemingly typing too much by by fixing the cell heights. However, if they type a "return", then it creates a new line and messes up formatting (I am trying to restrict total input to a single page).

Ideally, I would like to prevent the newline or better make it behave like the tab key and move to the next formfield. They don't need a newline since text wraps.

Thank you
-dorian

macropod
05-28-2009, 08:58 PM
Hi dorian,

You really shouldn't try to hijack other peoples' threads, even if they're dormant. You should start a new thread. In any event, there's plenty of code already available (even from Microsoft) for getting Word to treat the Enter key the same as it treats the Tab key in a formfield (see http://support.microsoft.com/kb/211219).

dorian
05-28-2009, 09:49 PM
yeah, sorry. On other forums, people gripe if we don't reuse existing threads for the same topic. I'm new here, so I wasn't sure. won't repeat. Thanks for the info!

-dorian