How to place cursor at the end following Setfocus?

  • Thread starter Thread starter plh
  • Start date Start date
P

plh

Hello Everyone:
The following code works very well except that the user has to keep clicking the
end of the string in the text box in order to add to it.
What I am really after is that after each entry into the text box, the list
(it's a continuous forms type of view) is progressively narrowed down. I seem to
recall that there is a simple way to make the cursor position itself at the end
of the string, but I'll be damned if I can find it in the MS help at this point!

Private Sub txtFilter_Change()
Dim strSQL As String

Debug.Print Me.txtFilter.Value


strSQL = "SELECT * FROM tblCustOrds WHERE COP LIKE '" & Me.txtFilter.Value &
"*'"

Me.RecordSource = strSQL
Me.txtFilter.SetFocus


End Sub

I also can't help but notice that on the first "shot", Debug.Print
Me.txtFilter.Value shows up "" and so there is no filtering effect until the
second shot. It may be relevant that I also have an after update event:

Private Sub txtFilter_AfterUpdate()
Call txtFilter_Change
End Sub

Any help will be greatly appreciated!
-plh
 
Hello Everyone:
The following code works very well except that the user has to keep clicking the
end of the string in the text box in order to add to it.
What I am really after is that after each entry into the text box, the list
(it's a continuous forms type of view) is progressively narrowed down. I seem to
recall that there is a simple way to make the cursor position itself at the end
of the string, but I'll be damned if I can find it in the MS help at this point!

Do you mean you want to set the Cursor location at the end of whatever is in txtFilter? If so, you can do this:

Me.txtFilter.SetFocus
Me.txtFilter.SelStart = Len(nz(Me.txtFilter, ""))

Private Sub txtFilter_Change()
Dim strSQL As String

Debug.Print Me.txtFilter.Value


strSQL = "SELECT * FROM tblCustOrds WHERE COP LIKE '" & Me.txtFilter.Value &
"*'"

Me.RecordSource = strSQL
Me.txtFilter.SetFocus


End Sub

I also can't help but notice that on the first "shot", Debug.Print
Me.txtFilter.Value shows up "" and so there is no filtering effect until the
second shot. It may be relevant that I also have an after update event:

Private Sub txtFilter_AfterUpdate()
Call txtFilter_Change
End Sub

Any help will be greatly appreciated!
-plh

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
[snip]

Do you mean you want to set the Cursor location at the end of whatever is in
txtFilter? If so, you can do this:

Me.txtFilter.SetFocus
Me.txtFilter.SelStart = Len(nz(Me.txtFilter, ""))

[snip]
Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com

Thank you, Scott, it did reset the position but now I have another problem:
Here is the new code:

Private Sub txtFilter_Change()
Dim strSQL As String

Debug.Print Me.txtFilter.Value

strSQL = "SELECT * FROM tblCustOrds WHERE COP LIKE '" & Me.txtFilter.Value &
"*'"

With Me
.Refresh
.RecordSource = strSQL
.txtFilter.SetFocus
On Error Resume Next
.txtFilter.SelStart = Len(.txtFilter)
End With

End Sub

strSQL does not contain the most recent entry into the text box, for example, if
the user types "1", nothing happens, because at that point strSQL contains
"SELECT * FROM tblCustOrds WHERE COP LIKE '*'". Then if the user types "2" so
that the text box says "12", strSQL contains "SELECT * FROM tblCustOrds WHERE
COP LIKE '1*'" so now it filters those records where the first digit is equal to
1. In other words, the filtering action always lags one step behind what the
user has entered into the box. Is there anything that will alleviate that
effect?

Thank You,
-plh
 
Back
Top