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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selstart not working 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
Anyone know why my Selstart should have stopped working. I am using this in the controls got focus event. I even tried it in the previos controls on exit, adding line to setfocus first.

Me.CommentsIn.SelStart = Len(Trim(Me.CommentsIn.Text)) + 1

Thanks
 
What about this ?
Me!CommentsIn.SelLength = 0
Me!CommentsIn.SelStart = Len(Trim(Me!CommentsIn.Value)) + 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Actually, Me.CommentsIn.SelStart = Len(Me.CommentsIn.Text1) should work just fine. Don't know why, but you don't need to add the + 1 part! I'm just guessing here, but if CommentsIn is a Memo field, your code will bomb if the Memo field is over half full, i.e. if the length is over 32767 characters. That's because Len takes an integer as a value. I use the following to get around this. Don't like using the much maligned SendKeys, but I've never been able to figure out an alternative. The SendKeys function simulates pressing <Control> + <End>.

Code:
If Not IsNull(CommentsIn) Then
     If Len(CommentsIn) < 32767 Then
         CommentsIn.SelStart = Len(CommentsIn) 
     Else
        SendKeys "^{END}"
    End If
End If

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
missinglinq said:
[blue]your code will bomb if the Memo field is over half full, i.e. if the length is over 32767 characters. [purple]That's because Len takes an integer as a value.[/purple][/blue]
[ol][li][blue]Len[/blue] returns a [blue]Long Integer[/blue]![/li]
[li]The problem is [blue]SelStart[/blue] is limited to 32767 . . .[/li][/ol]

Calvin.gif
See Ya! . . . . . .
 
Thanks for replies, sorry for delay. I now have a regular scenario which shows my problem. If I put 2 text boxes on a form, the first being visable the second being hidden. I open the form, fill in textbox1, press enter. In the exit code of textbox1, I havo code to make textbox2 visible, and as I want that texbox entered into next, I put in a setfocus statement. That all works, and I type into textbox2. When finished, I press return, it returns me to textbox1 (as both boxes tab ordered). When I press return again, I move to textbox2, but my text previously entered is fully highlighted. If I put a selstart line in the onfocus event of textbox2, it still highlights all the text???? Any ideas of a workround, or any answers? Thanks

Private Sub Text0_Exit(Cancel As Integer)
Me.Text2.Visible = True
Me.Text2.SetFocus
End Sub

Private Sub Text2_GotFocus()
If IsNull(Me.Text2.Text) = False Then
Me!Text2.SelLength = 0
Me.Text2.SelStart = Len(Trim(Me.Text2.Text)) + 1
End If
End Sub
 
And what about this ?
Private Sub Text2_GotFocus()
Me!Text2.SelLength = 0
Me!Text2.SelStart = Len(Trim(Me!Text2 & "") + 1
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, but no change. I had to remove the +1 as it rejected it. Its giving me a real headache. All I have is a form and two unbound textboxes in the sequence/code described, and it's giving me this problem.
 
It seems:
If the two texboxes are set visible to begin with, and the text2.setfocus is ommited from the text1_exit property, it all works okay. But if I have text2 hidden to start, and the text2 setfocus in text1_exit, it fails.
 
And this ?
Private Sub Text0_Exit(Cancel As Integer)
Me!Text2.Visible = True
Me!Text2.SetFocus
DoEvents
Me!Text2.SelLength = 0
Me!Text2.SelStart = Len(Trim(Me!Text2 & "")
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Sorry PHV, still does not work.
 
I think this is overpopulating the form with code.

Here I'm assuming the first text control needs to have something entered too - but one clue, I think, is which event - after update vs on exit.

[tt]Private Sub Text0_After_Update()

myToggle

End Sub

Private Sub Text2_GotFocus()

Me!Text2.SelStart = Len(Me!Text2.Value & vbNullString)

End If

Private Sub myToggle()

Me!Text2.Visible = (Len(Me!Text2.Value & vbNullString) > 0)

End Sub

Private Sub Form_Current()

myToggle

End Sub[/tt]

If none of this work, I'd suggest creating a new db, new form, using minimal code/controls and try to replicate the behaviour. Make that work, then try to implement in the original. The results of that, would indicate whether there are corruption issues at hand.

Roy-Vidar
 
Thanks Roy, however:

Text0_After_Update()???, should that not be Text0_AfterUpdate()

Also

Private Sub Text2_GotFocus()

Me!Text2.SelStart = Len(Me!Text2.Value & vbNullString)

End If (should it be end sub??)

What do you get on your machine if you use my code?, can you or someone else just try two text boxes, unbound, on a form, and paste my code in and tell me what they get. Do you get the same scenario??? Thanks
 
Howdy ZOR! . . .

I Setup the [blue]same secnario[/blue] and got the [blue]same results![/blue]

After some testing it became apparent that the code . . .
Code:
[blue]   Me.Text2.Visible = True
   Me.Text2.SetFocus[/blue]
. . . just doesn't allow [blue]SelStart[/blue] to work properly if it resides in either the [blue]OnExit/OnLost Focus events of Text0![/blue] I can't say why (apparently there's some problem when the focus is in transit while either event is still running) [red]but it sure wont work! . . .[/red] So these events can't be used . . .

I transferred the code to the [blue]On KeyDown[/blue] event of Text0 and detecting the [blue]Return & Tab keys[/blue] it works just fine!
Code:
[blue]Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
   [green]'Tab = 9, Return = 13[/green]
   If (KeyCode = 9 Or KeyCode = 13) And (Me!Text2.Visible = False) Then
      Me.Text2.Visible = True
      Me.Text2.SetFocus
   End If

End Sub

Private Sub Text2_GotFocus()
   If Not IsNull(Me!Text2) Then
      Me!Text2.SelStart = Len(Trim(Me!Text2))
   End If
   
End Sub[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
Many thanks for the confirmation, I thought I was going mad with such a simple problem. I had just finished trying it out on Access97 and got same results. Thanks a lot for working on it, and getting a result that works. Surprised nobody else has had it. As you say, if Text2 is not hidden, and there is no setfocus code in the previous textbox, and it just moves by virtue of tab order then theres no problem. I got stuck with it because the user wanted a step by step form fill. I was considering having Text2 visible, and having its width set to zero until it was to be active. Have a well earned star, hope someone else does not fall in this hole/headache. Regards
 
Just an extra thankyou. I have just used your suggestion in 3 texboxes, all working fine. Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top