Help, How to keep the cursor position in a textbox (1 Viewer)

Louislam

Registered User.
Local time
Today, 06:45
Joined
Feb 7, 2014
Messages
13
Hi,

I have below code for maximum 3 criteria dynamic search:
Code:
Private Sub SearchFor_Change()
    Dim strWhere As String
    Dim Criteria As String
    Dim i As Integer
    Dim A
    Dim strAdd As String
    
    On Error Resume Next
    strAdd = Me.SearchFor.Text
    Me.SearchFor.Value = strAdd
    Criteria = "[Materials_Name] & [Materials_Specification]"
    
    i = (Len(Me.SearchFor) - Len(Replace(Me.SearchFor, ";", ""))) / Len(";")
    Select Case i
        Case 0
                  strWhere = Criteria & "Like '*" & strAdd & "*'"

        Case 1
            A = Split(Me.SearchFor.Text, ";")
                 strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*'"
                  
        Case 2
            A = Split(Me.SearchFor.Text, ";")
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And" & Criteria & "Like '*" & A(2) & "*'"
           
    End Select
    
    Me.SearchFor.SetFocus
    Me.SearchFor.SelStart = Me.SearchFor.SelLength
    Me.Materials_Records_Inner.Form.Filter = strWhere
    Me.Materials_Records_Inner.Form.FilterOn = True
    
    Call Count_Materials_No

End Sub

There is a problem, since I put the code in a textbox's on change event. then in case i want to revise the first two criteria, the cursor will automatically fleets to the end of the text, I want to know how to keep the position of the cursor?
Thanks
 

MarkK

bit cruncher
Local time
Today, 06:45
Joined
Mar 17, 2004
Messages
8,186
I would comment out these lines of code . . .
Code:
    Me.SearchFor.Value = strAdd   [COLOR="Green"]'this gets set automatically later[/COLOR]
[COLOR="Green"]    ' and further down . . .[/COLOR]
    Me.SearchFor.SetFocus      [COLOR="Green"]'the control already has the focus during Change[/COLOR]
    Me.SearchFor.SelStart = Me.SearchFor.SelLength   [COLOR="Green"]'leave the insertion point alone[/COLOR]
See what difference that makes.
 

Louislam

Registered User.
Local time
Today, 06:45
Joined
Feb 7, 2014
Messages
13
Hi Lagbolt,
I tried to make them as comment but it doesn't work. In this way, it lose the multiple criteria function, that means I only have one criteria for searching

By the way, here is the full code in my database,
Code:
Private Sub SearchFor_Change()
    Dim strWhere As String
    Dim strWhere1 As String
    Dim Criteria As String
    Dim I As Integer
    Dim A
    Dim strAdd As String
    
    On Error Resume Next
    strAdd = Me.SearchFor.Text
    Me.SearchFor.Value = strAdd
    Criteria = "[Sub_Type] & [Fabric_IC] & [Fabrication] & [Article_No] & [Construction] & [Yarn_Size] & [Composition] & [Weigth_BW] & [Country_Of_Origin] & [Quotation_Day]"
    
    I = (Len(Me.SearchFor) - Len(Replace(Me.SearchFor, ";", ""))) / Len(";")

    Select Case I
        Case 0
            strWhere1 = Criteria & "Like '*" & strAdd & "*'"
            If IsNull(Me.strType) = True Then
                  strWhere = Criteria & "Like '*" & strAdd & "*'"
            Else
                  strWhere = Criteria & "Like '*" & strAdd & "*' And " & Me.strType.Value
            End If
        Case 1
            A = Split(Me.SearchFor.Text, ";")
            strWhere1 = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*'"
            If IsNull(Me.strType) = True Then
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*'"
            Else
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And " & Me.strType.Value
            End If
                  
        Case 2
            A = Split(Me.SearchFor.Text, ";")
            strWhere1 = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And" & Criteria & "Like '*" & A(2) & "*'"
            If IsNull(Me.strType) = True Then
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And" & Criteria & "Like '*" & A(2) & "*'"
            Else
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And" & Criteria & "Like '*" & A(2) & "*' And " & Me.strType.Value
            End If
           
    End Select
    
    Me.SearchFor.SetFocus
    Me.SearchFor.SelStart = Me.SearchFor.SelLength
    Me.Materials_Records_Inner.Form.Filter = strWhere
    Me.Materials_Records_Inner.Form.FilterOn = True
    
    Call Count_Materials_No
    Me.SrchText.Value = strWhere1

End Sub

The problem is still pending!:(
 

spikepl

Eledittingent Beliped
Local time
Today, 15:45
Joined
Nov 3, 2010
Messages
6,142
Leave your search textbox alone while mucking about ... :D

Code:
Private Sub SearchFor_Change()
    Dim strWhere As String
    Dim strWhere1 As String
    Dim Criteria As String
    Dim I As Integer
    Dim A
    Dim strAdd As String
    
    On Error Resume Next
    strAdd = Me.SearchFor.Text
'    Me.SearchFor.Value = strAdd ' here you mess up your textbox, unnecessarily, as also  per Mark's comment. Leave it be ...
    Criteria = "[Sub_Type] & [Fabric_IC] & [Fabrication] & [Article_No] & [Construction] & [Yarn_Size] & [Composition] & [Weigth_BW] & [Country_Of_Origin] & [Quotation_Day]"
    
    I = (Len(strAdd ) - Len(Replace(strAdd , ";", ""))) / Len(";")

    Select Case I
        Case 0
            strWhere1 = Criteria & "Like '*" & strAdd & "*'"
            If IsNull(Me.strType) = True Then
                  strWhere = Criteria & "Like '*" & strAdd & "*'"
            Else
                  strWhere = Criteria & "Like '*" & strAdd & "*' And " & Me.strType.Value
            End If
        Case 1
            A = Split(strAdd , ";")
            strWhere1 = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*'"
            If IsNull(Me.strType) = True Then
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*'"
            Else
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And " & Me.strType.Value
            End If
                  
        Case 2
            A = Split(strAdd , ";")
            strWhere1 = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And" & Criteria & "Like '*" & A(2) & "*'"
            If IsNull(Me.strType) = True Then
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And" & Criteria & "Like '*" & A(2) & "*'"
            Else
                  strWhere = Criteria & "Like '*" & A(0) & "*' And" & Criteria & "Like '*" & A(1) & "*' And" & Criteria & "Like '*" & A(2) & "*' And " & Me.strType.Value
            End If
           
    End Select
    
'''''''''''''''''''''''    Me.SearchFor.SetFocus
'''''''''''''''''''''''    Me.SearchFor.SelStart = Me.SearchFor.SelLength
    Me.Materials_Records_Inner.Form.Filter = strWhere
    Me.Materials_Records_Inner.Form.FilterOn = True
    
    Call Count_Materials_No
    Me.SrchText.Value = strWhere1

End Sub
 

Louislam

Registered User.
Local time
Today, 06:45
Joined
Feb 7, 2014
Messages
13
Oh, thanks lagbolt and spikepl, You are so brilliant, that finally works.
Thanks millions:D:D:D
 

Users who are viewing this thread

Top Bottom