Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter key selects text rather than create new line

Status
Not open for further replies.

annie52

Technical User
Mar 13, 2009
164
US
I have a text box bound to a memo field. In the GotFocus event I set the SelStart and SelLength properties to false and the EnterKeyBehavior to Add New Line.

When I click in the text box, then press Ctrl+End, I move to the end of the text. So far, so good. But, regardless of whether I use Enter or Ctrl+Enter to add a new line (vbcrlf), a good portion of the text is deleted and my cursor moves to the top of the remaining text. Of course, the Escape key brings back the missing text but it's pretty scary.
 
Hi Remou. I had already done that. (I just used the wrong words: "Add New Line" instead of "New Line in Field"). Any other ideas?
 
Please post your code. It seems that you are selecting a section of text rather than moving to a point. SelLength should probably be zero.

 
Private Sub DisrepComments_GotFocus()
On Error GoTo Err_DisrepComments_GotFocus

Me.DisrepComments.SelLength = False
Me.DisrepComments.SelStart = False

Exit_DisrepComments_GotFocus:
Exit Sub

Err_DisrepComments_GotFocus:
Call RPPErrorHandler
Resume Exit_DisrepComments_GotFocus

End Sub

Actually, I started out setting both properties to zero. When that didn't correct the behavior I tried with the code above.
 

Also be aware that you should always place this type of code in the OnClick event as well as in the GotFocus event of the control.

Some people say having it just in OnFocus works when clicking into the field while others (myself included) find that it doesn't, and hence the need to place the code in both events.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Hi Remou and missinglinq. I tried Remou's code but it made no difference. It does move the cursor to the end of the field but when I press the Enter key to obtain a new line, the text on previous lines is selected and deleted.

If and when, I get this resolved I won't want to use it in the Click event. The code will always put my cursor at the end of the field and not allow me to edit the comments. I'm at my wit's end and terriby afraid of the data that's going to be chewed up and spit out. Any other ideas?
 
I don't know if this will help generate ideas but --

The problem with the Enter key selecting and deleting text only happens when you first enter into the field. I tab, enter, or click in the DisrepComments field, Ctrl+End to get to the end of the field, and press Enter. Most of the text "disappears" and I can bring it back by pressing the Esc key. After jumping through these hoops, the Enter key behaves as expected; it creates a new line.

The problem with the Enter key occurs whether or not I use SelStart and SelLength in my code.

I considered the possibility of corruption, created a new blank database, linked the tables, and imported the queries, forms, reports, code, menu, etc. The Enter key problem still exists.

I welcome any and all ideas no matter how crazy them may seem.
 
I just deleted the DisrepComments field, did a compact & repair, and then recreated the field but this did not correct the problem.

I didn't think to mention earlier that this problem is unique to this particular subform. I have four subforms in a tab control that are linked to the parent form. All four subforms have a comments field bound to a memo field in a table. The Enter key behaves as expected in the fields on the other three subforms.
 

The only thing I see that is odd is in the order of the code:
Code:
Me.DisrepComments.SelLength = 0
Me.DisrepComments.SelStart = Len(Me.DisrepComments)

Normally this would be the reverse:
Code:
Me.DisrepComments.SelStart = Len(Me.DisrepComments)
Me.DisrepComments.SelLength = 0

Might be worth trying.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Hi Missinglinq. The order makes no difference in the Enter key behavior. At least, not in my situation. I have finally given up on this mystery. After going back more than a week, I found a backup copy of the FE that works properly. Thanks to you and Remou for standing by me through this.
 
I've been playing with the various events behind the form in question and discovered that, if I comment out the following code in the Dirty event, the Enter key behaves appropriately.

'Pass the value of ReqnID to frmDisreps for linking purposes.
'If user attempts to enter data into the subform before entering a reqn #
'in the main form, display a message and clear the subform.

If IsNull(Forms![frmReqnWatch]![ReqnID]) Then
MsgBox "Please enter a requisition number before" & vbCrLf _
& "keying in the discrepancy information.", vbOKOnly, "RPP Tracking System"
Cancel = True
Forms![frmReqnWatch]![JD].SetFocus
Else
Forms![frmReqnWatch]![frmDisreps].Form![XrefReqnID] = Forms![frmReqnWatch]![ReqnID]
End If

I don't get it. Does anybody see why this code could have caused the problem?
 
Sorry, I forgot to come back and post a resolution. I have a fairly complicated query underlying this form. One of the tables in the query was actually unnecessary. Once I removed it and cleaned up my joins, the problem disappeared!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top