Problem typing searh text field (3 Viewers)

LjushaMisha

Registered User.
Local time
Today, 17:26
Joined
Mar 10, 2017
Messages
68
Hi.
I have following Sub for On Change event:


Private Sub txtSearch_Change()
Me.txtDummy = Me.txtSearch.Text

Dim db As Database
Dim rs As Recordset
Dim sql As String

sql = "SELECT tblProducts.* " & _
"FROM tblProducts " & _
"WHERE tblProducts.ProductName Like '*" & [Forms]![frmFind]![txtDummy] & "*'"

Set db = CurrentDb
Set rs = db1.OpenRecordset(sql1, dbOpenDynaset)
Set Forms!frmFind!sfrmResults.Form.Recordset = rs1
End Sub

Until I have two "divided" forms (frmFind and frmResults) I have no problem typing letters in the txtSearch control on frmSearch. Of course Sub is different in last line:
(Set Forms!sfrmResults.Recordset = rs1).

When I make form "sfrmResults" subform of "frmFind" then problems occurs while typing my criteria in control txtSearch. (Set Forms!frmFind!sfrmResults.Form.Recordset = rs1).
AFTER EACH LETTER I TYPE CURSOR MOVES AT THE BIGINING OF SEARCH STRING (instead of remaining after last entered letter.

For example: if I want to type MILK it comes out M, IM, LIM, KLIM. (i.e.reversed)

Any idea of what am I doing wrong?
 
It is because the control loses the focus. You need to get the controls sel properties- selstart,sellengh etc and note their values just before losing focus and on regaining focus set them again

On my phone so can’t provide the code - perhaps someone else will if you can’t work it out
 
I don't think you mentioned it, but did you say what was the error with your code/procedure?
 
Why are you dimming db and rs, then using db1 and rs1?
Do you have Option Explicit in your modules?
 
It is because the control loses the focus. You need to get the controls sel properties- selstart,sellengh etc and note their values just before losing focus and on regaining focus set them again

On my phone so can’t provide the code - perhaps someone else will if you can’t work it out
THanks. You nailed it.

I've modified a code just a litle bit. Something like:
Inserted new first line:
txtSearch.selstart = 0
and added 2 lines before End Sub line like:
Me.txtSearch.SetFocus
txtSearch.SelStart = Len(txtSearch)

WORKING GREAT.
THANKS AGAIN
 
Use copy/paste - more accurate and saves everyone’s time including yours. And use code tags to preserve formatting
I know. But as my names are not English based I only wanted to make code easy to read/understand for others. Clearly mistake.
Sorry. Nex time I will try to follow your advice. Sorry again
 
THanks. You nailed it.

I've modified a code just a litle bit. Something like:
Inserted new first line:
txtSearch.selstart = 0
and added 2 lines before End Sub line like:
Me.txtSearch.SetFocus
txtSearch.SelStart = Len(txtSearch)

WORKING GREAT.
THANKS AGAIN
Sorry, HAPPY TOO SOON.
At the end I finished with this part of code: (after getting error "invalide" us of Null)

If IsNull(Len(txtSearch)) Then
txtSearch.SelStart = 0
Else
txtSearch.SelStart = Len(txtSearch)
End If

BUT (I think there is always BUT) --> BUT while I'm typing search contition in txtSearch control and I press SPACE BAR it seams that len() function doesn't count this space as character and "moves" cursor after the last "real" character. Any idea of solution?
 
Len(txtSearch) is never going to be Null. It will be 0 or the length of the text in the control.
 
Len(txtSearch) is never going to be Null. It will be 0 or the length of the text in the control.
Not sure. If txtSearch is EMPTY (which is when I load the form or clear the txtSearch control for new search) and I type "? Len(Forms!frmFind!txtSearch)" in Immediate window I get "answer" Null. So ...
 
Not sure. If txtSearch is EMPTY (which is when I load the form or clear the txtSearch control for new search) and I type "? Len(Forms!frmFind!txtSearch)" in Immediate window I get "answer" Null. So ...
So it is :(
My apologies.
Perhaps try Len(Me.txtSearch) & "" = 0
 
Sorry, HAPPY TOO SOON.
At the end I finished with this part of code: (after getting error "invalide" us of Null)

If IsNull(Len(txtSearch)) Then
txtSearch.SelStart = 0
Else
txtSearch.SelStart = Len(txtSearch)
End If

BUT (I think there is always BUT) --> BUT while I'm typing search contition in txtSearch control and I press SPACE BAR it seams that len() function doesn't count this space as character and "moves" cursor after the last "real" character. Any idea of solution?
FOUND SOLUTION.
Of course I have also txtSearchDummy control in same form. I have noticed that while typing "space" this space is "registered" in ...Dummy control.
So I inserted another line of code after Else line:
Me.txtSearch = Me.txtSearchDummy

Now IT WORKS
Now it is SOLVED
Thx to everybody for their help. Their help made me work on this problem for only two days.:unsure::):ROFLMAO::ROFLMAO::ROFLMAO::ROFLMAO::eek:
 

Users who are viewing this thread

Back
Top Bottom