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

Manipulating position of cursor in textbox 1

Status
Not open for further replies.

anet

Programmer
Jul 10, 2001
35
CA
The following snippet of code works fine, except after execution the cursor positions at the beginning of the textbox instead of where the user left off. What I want is for the user to be able to keep entering the name in case the name that the database finds isn't the one they want.

Private Sub txtLName_Change()

Dim strName As String
Dim strField As String
Dim rs As ADODB.Recordset

strField = "LName"
strName = txtLName.Text

Set rs = GetArtist(strName, strField)
If Not rs.EOF Then
With rs
.MoveFirst
txtLName.Text = !LName
txtFName.Text = !FName
txtArtistID.Text = !artistID
End With
End If

Set rs = Nothing
End Sub

How do I tell VB not to resposition the cursor? Thanks
 
Try this

Text2.SelStart = Len(Trim(Text2.Text))
Text2.SetFocus

The selstart position is derived from the length of the text entered so far
 
That doesn't really work, because if the database returns a record the cursor goes to the end of the text. Here is what I am attempting to do:
-User types the first letter of the name in the textbox
-change event fires and queries the database using the like operator to find all names that match the first letter and enters the first record into the textbox (my code works fine to this point)
-then if this is not the name the user wants, they should be able to type in the next letter of the name (which should clear everything after the cursor) the change event fires again and this time finds records that match the first 2 letters of the name, etc., etc. (this is the part I can't get to work)
Any ideas how I can accomplish this? Thanks.
 
Maybe you can acomplish this by counting the number of characters actually entered by the user and then use the SelStart property to position the cursor.
Rum am Morgen vorkommt Kummer und Sorgen... Cheers !

Mr. Rum
 
TRy
If Not rs.EOF Then
Dim intSelStart as Integer
With rs
.MoveFirst
intSelStart = textLName.SelStart
txtLName.Text = !LName
textLName.SelStart = intSelStart

intSelStart = textFName.SelStart
txtFName.Text = !FName
textFName.SelStart = intSelStart

intSelStart = txtArtistID.SelStart
txtArtistID.Text = !artistID
txtArtistID.SelStart = intSElStart
End With
End If
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Try this:

Private Sub txtLName_Change()

Dim strName As String
Dim strField As String
Dim rs As ADODB.Recordset
Dim lngOrigPos As Long

strField = "LName"
strName = txtLName.Text
lngOrigPos = txtLName.SelStart

Set rs = GetArtist(strName, strField)
If Not rs.EOF Then
With rs
.MoveFirst
txtLName.Text = !LName
txtFName.Text = !FName
txtArtistID.Text = !artistID
txtLName.SelStart = lngOrigPos
txtLName.SelLength = Len(txtLName.Text)

End With
End If

Set rs = Nothing
End Sub


Chris.
 
This is greats, I have one question here, can any one tell me that how do I set the cel cursor position start from he begining in the text box when I click in any position in my text box.

If I have very long text box I may click at the middle or near to the end position of the text box but I want the cel cursor automatically run to the beginig of the text box.

[pipe]
 
Text1.SelStart = 0


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Here is my code. I am basically trying to get the same effect, though instead of searching I am filtering a subform. I tried adding just:

Dim lngOrigPos As Long
lngOrigPos = txtLName.SelStart

but that made the program think that the cursor was "entering the field", and so followed the behavior I have set in Options under keyboard (either select entire field, or go to end of field, etc).

Is there something else I need to add to get the cursor to stay in the same location?

Thanks



Private Sub ShipFilter_KeyUp(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case Is = 13 ' Ignore Return Key - If appropriate
Case Is = 37 ' Ignore Left Arrow
Case Is = 38 ' Ignore Up Arrow
Case Is = 39 ' Ignore Right Arrow
Case Is = 40 ' Ignore Down Arrow
Case Else
'Your code to respond to all other keys here
On Error GoTo Err_ShipFilter_KeyUp
Dim stDocName As String
stDocName = "OpenStuff.Requery"
DoCmd.RunMacro stDocName
ShipFilter.SetFocus
ShipFilter.SelStart = 255


End Sub
 
Worked in a bit of code from the Microsoft support page, which seems to help:

Dim stDocName As String
npos = ShipFilter.SelStart
stDocName = "OpenStuff.Requery"
DoCmd.RunMacro stDocName
ShipFilter.SetFocus
ShipFilter.SelStart = npos

 
Question:

Does anyone know if this same cursor position sequence can be set into a DataGrid....I have tried everything...the SelStart Selects the appropriate text but the cursor is ALWAYS at the end of the Selected text within the DataGrid Cell.....is this just standard with DataGrid navigation??

Here is my code
Code:
Private Sub InspectionGrid_Change()
    
    ' Variables
        Dim Partial As Variant
        Dim OrigPos As Long
        
    ' RecordSets
        Dim rsCol As New ADODB.Recordset
        
    ' Open Recordsets
        rsCol.Open "Select [FieldValue] From Collections Where [FieldName] = '" & InspectionGrid.Columns(InspectionGrid.Col).DataField & "' Order By [FieldName]", Cn
        
    ' Partial Variable set to current DataGrid cell text
        Partial = InspectionGrid.Text
        Debug.Print Partial
    ' Check to make sure DataGrid cell contains valid value
        If Partial <> &quot;&quot; Then
        ' Find closest stored value to Partial string variable
            rsCol.Find &quot;FieldValue Like '&quot; & Partial & &quot;%'&quot;
            OrigPos = InspectionGrid.SelStart
        ' If value is found set it equal to Total string variable
            If rsCol.EOF <> True Then
                Partial = rsCol!FieldValue
                With rsCol
                    If ShowText <> False Then
                        InspectionGrid.Text = !FieldValue
                        InspectionGrid.SelStart = OrigPos
                        InspectionGrid.SelLength = Len(Partial)
                    End If
                End With
            End If
        End If
        
End Sub
[\code]

-vza
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top