jackrabbitman
asked on
SetFocus Event doesn't work
Alright well....I need to use the .text property to update my the recordSource on my form, but having to use a property means that the textbox must have focus first...so easy enough I used the Me.Textbox.SetFocus before the event, but it only works on certain occasions, and I can't determine when it works and when it doesn't. Any idea why the setfocus event wouldn't work because I get the Runtime error 2185 "You cant reference a property or control that doesn't have the focus." The code is run in the OnChange event and runs through a series of textboxes to make a SQL statement based off of them. Any help would be greatly appreciated.
Thanks
Thanks
ASKER
https://www.experts-exchange.com/questions/24516458/How-can-you-get-a-query-to-update-every-keystroke.html
that should provide some background....In the mean time I'll take your advice and see how that works out....
that should provide some background....In the mean time I'll take your advice and see how that works out....
"setfocus event "
Just noting there is not a 'set focus' event, but instead a SetFocus 'method'
mx
Just noting there is not a 'set focus' event, but instead a SetFocus 'method'
mx
ASKER
just tried it with Me.textbox after I moved the sub to the same module as the form...and I ran into the same problem as before....with the value being returned instead of the text. I'll put my code down here to maybe help explain a little bit more.
I see you posted while I was putting up this post so...here is the code at least.
I see you posted while I was putting up this post so...here is the code at least.
Private Sub cname_Change()
searchCriteria
End Sub
Sub searchCriteria()
Dim SQL1 As String
Dim SQL2 As String
Dim SQL3 As String
Dim SQL4 As String
Dim SQL5 As String
Dim criteria As String
Dim length As Integer
Dim condition As Boolean
criteria = ""
condition = False
'SQL statements
SQL1 = "SELECT blah blah blah
SQL2 = "UNION ALL SELECT blah blah blah
SQL3 = "UNION ALL SELECT blah blah blah
SQL4 = "UNION ALL SELECT blah blah blah
SQL5 = "UNION ALL SELECT blah blah blah
'Company Name Criteria
Forms!Search!cname.SetFocus
If Not IsNull(Forms!Search!cname.Text) Then
length = Len(Forms!Search!cname.Text)
criteria = "(((Left(CompanyName, " & length & ")) = [Forms]![Search]![cname])) "
condition = True
End If
'First Name Criteria
Forms!Search!firstname.setfocus
If Not IsNull(Forms!Search!firstname.Text) And criteria = "" Then
length = Len(Forms!Search!firstname.Text)
criteria = "(((Left(Fname, " & length & ")) = [Forms]![Search]![firstname])) "
condition = True
ElseIf Not IsNull(Forms!Search!firstname.Text) Then
length = Len(Forms!Search!firstname.Text)
criteria = criteria & "AND (((Left(Fname, " & length & ")) = [Forms]![Search]![firstname])) "
End If
'Last Name Criteria
Forms!Search!lastname.SetFocus
If Not IsNull(Fomrs!Search!lastname.Text) And criteria = "" Then
length = Len(Forms!Search!lastname.Text)
criteria = "(((Left(Lname, " & length & ")) = [Forms]![Search]![lastname])) "
condition = True
ElseIf Not IsNull(Forms!Serach!lastname.Text) Then
length = Len(Forms!Search!lastname.Text)
criteria = criteria & "AND (((Left(Lname, " & length & ")) = [Forms]![Search]![lastname])) "
End If
If condition Then
Forms!Search.RecordSource = SQL1 & "WHERE" & criteria & SQL2 & "WHERE" & criteria & SQL3 & "WHERE" & criteria & SQL4 & "WHERE" & criteria & SQL5 & "WHERE" & criteria
Else
Forms!Search.RecordSource = SQL1 & SQL2 & SQL3 & SQL4 & SQL5
End If
End Sub
OK ... let me give you an example of how the On Change event would be used in conjunction with the .Text property of a specific text box:
Private Sub text1_Change()
Const sChrNotAllowd = " /\[]:|<>+=;,?* "
With Me
If .Text1.Text = "" Then Exit Sub
If InStr(sChrNotAllowd, Right(.Text1.Text, 1)) > 0 Then
Beep
.Text1.Text = Left(.Text1.Text, Len(.Text1.Text) - 1)
SendKeys "{BACKSPACE}"
Exit Sub
End If
End With
End Sub
In this case, there is no issue with the 'focus', because the text box DOES have the Focus each time a key is pressed and the event triggers. Notice how the .Text property is being used. It continually updates with the 'value' ( not to be confused with the .Value property) each time a key is pressed. Again (from the other Q), the .Value property (the default prop of the control) does not update until you exit the control.
mx
Private Sub text1_Change()
Const sChrNotAllowd = " /\[]:|<>+=;,?* "
With Me
If .Text1.Text = "" Then Exit Sub
If InStr(sChrNotAllowd, Right(.Text1.Text, 1)) > 0 Then
Beep
.Text1.Text = Left(.Text1.Text, Len(.Text1.Text) - 1)
SendKeys "{BACKSPACE}"
Exit Sub
End If
End With
End Sub
In this case, there is no issue with the 'focus', because the text box DOES have the Focus each time a key is pressed and the event triggers. Notice how the .Text property is being used. It continually updates with the 'value' ( not to be confused with the .Value property) each time a key is pressed. Again (from the other Q), the .Value property (the default prop of the control) does not update until you exit the control.
mx
ASKER
thanks for the help with the .text property and the funtionality has improved, but I'm still getting the runtime error 2185 "You can't reference a property or method for a control unless the control has the focus" which once it happens once, happens everytime...and I still have the SetFocus before every if statement because each textbox needs to be checked and any textbox can call the code...what I don't understand is that when the form loads and you run it for the first time, there is never a problem and then all of the sudden it stops working and brings up the Runtime error. Right now I'm just trying to get one text box set up still so its not like there's even data in the other textboxes that could be messing things up. Latest code is below.
Sub searchCriteria()
With Me
.cname.SetFocus
If .cname.Text <> "" Then
length = Len(.cname.Text)
criteria = "(((Left(CompanyName, " & length & ")) = [Forms]![Search]![cname])) "
condition = True
End If
'First Name Criteria
.firstname.SetFocus
If .firstname.Text <> "" And criteria = "" Then
length = Len(.firstname.Text)
criteria = "(((Left(Fname, " & length & ")) = [Forms]![Search]![firstname])) "
condition = True
ElseIf .firstname <> "" Then
length = Len(.firstname.Text)
criteria = criteria & "AND (((Left(Fname, " & length & ")) = [Forms]![Search]![firstname])) "
End If
'Last Name Criteria
.lastname.SetFocus
If .lastname.Text <> "" And criteria = "" Then
length = Len(.lastname.Text)
criteria = "(((Left(Lname, " & length & ")) = [Forms]![Search]![lastname])) "
condition = True
ElseIf .lastname.Text <> "" Then
length = Len(.lastname.Text)
criteria = criteria & "AND (((Left(Lname, " & length & ")) = [Forms]![Search]![lastname])) "
End If
What line of code does the error occur on?
mx
mx
ASKER
If .cname.Text <> "" Then
I am currently just working on the cname textbox as well so it should work even without the SetFocus...which when it actually does work, it isn't needed like you said before.
I am currently just working on the cname textbox as well so it should work even without the SetFocus...which when it actually does work, it isn't needed like you said before.
Is cname the Name of the Control or the Control Source or both? If both, try give the control Name property txtcname so that there is a clear distinction as to what you are referencing.
So, it would look like this ... (Partial):
With Me
.txtcname.SetFocus
If .txtcname.Text <> "" Then
length = Len(.txtcname.Text)
criteria = "(((Left(CompanyName, " & length & ")) = [Forms]![Search]![cname])) "
condition = True
So, it would look like this ... (Partial):
With Me
.txtcname.SetFocus
If .txtcname.Text <> "" Then
length = Len(.txtcname.Text)
criteria = "(((Left(CompanyName, " & length & ")) = [Forms]![Search]![cname]))
condition = True
ASKER
I appreciate all of your help...
cname is an unbound text box, cname is the name of the control....It sits in the form header so that users can put in their search criteria. for the continuous form below which has a field of CompanyName soo I don't think that there could be any confusion...
cname is an unbound text box, cname is the name of the control....It sits in the form header so that users can put in their search criteria. for the continuous form below which has a field of CompanyName soo I don't think that there could be any confusion...
ASKER
would It be helpful If I cleaned up the database and posted it so that you could see what was actually happening?
yes ...
Can you:
1) Compact & Repair (*** to shrink the size),
2) Zip up the MDB (*** to further shrink the size)
3) Attach the file for upload here (using the 'Attach File function below) ... removing any sensitive data of course.
4**** And please give a clear explanation of exactly how to reproduce the problem or what you are trying to do. ********
mx
Can you:
1) Compact & Repair (*** to shrink the size),
2) Zip up the MDB (*** to further shrink the size)
3) Attach the file for upload here (using the 'Attach File function below) ... removing any sensitive data of course.
4**** And please give a clear explanation of exactly how to reproduce the problem or what you are trying to do. ********
mx
ASKER
Alright here it is
I have removed everything from the database except the search form and the main menu so it should be easy to find...I also left in a couple of sample records to make life easier for you (hopefully).
Umm like I said I don't know exactly what causes the problem to occur, but only the company name has been set up as you'll see. I've noticed that particularily if you enter information in thats not in the database and returns 0 results it tends to start the error. Once it starts then it doesn't stop either until you close the form. So just start entering in company names and then backspace them away with a couple of extra backspaces in there as well and it should start popping up. Its not hard to get it to pop up, but it is hard to explain exactly what causes it to happen.
Thanks, I really appreciate the help.
Contact-Database.zip
I have removed everything from the database except the search form and the main menu so it should be easy to find...I also left in a couple of sample records to make life easier for you (hopefully).
Umm like I said I don't know exactly what causes the problem to occur, but only the company name has been set up as you'll see. I've noticed that particularily if you enter information in thats not in the database and returns 0 results it tends to start the error. Once it starts then it doesn't stop either until you close the form. So just start entering in company names and then backspace them away with a couple of extra backspaces in there as well and it should start popping up. Its not hard to get it to pop up, but it is hard to explain exactly what causes it to happen.
Thanks, I really appreciate the help.
Contact-Database.zip
ok ... I have the mdb now ...back later.
mx
mx
ASKER
best of luck and I really appreciate all of the help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh ... if you go the Me.Filter route ... you need a
Me.FilterOn = True after the Me.Filter = <whatever>
Me.Filter = <whatever>
Me.FilterOn = True
mx
Me.FilterOn = True after the Me.Filter = <whatever>
Me.Filter = <whatever>
Me.FilterOn = True
mx
ASKER
thanks for the solution, i'll start working on that on monday...time for the weekend.
ASKER
Thanks for all the help
cool. see you down the road if you need help on that.
mx
mx
ASKER
Hey MX,
Hopefully you'll see this, but I posted another question about the whole setFocus ordeal and it not working here:
But the long and the short of it is that its a bug in Access that doesn't allow the .Text property to be referenced even if there is focus on the object (which I have now figured out that there is) so basically I'm at the mercy of the Access gods as far as if this will work or not.
Thanks again for the help.
Hopefully you'll see this, but I posted another question about the whole setFocus ordeal and it not working here:
But the long and the short of it is that its a bug in Access that doesn't allow the .Text property to be referenced even if there is focus on the object (which I have now figured out that there is) so basically I'm at the mercy of the Access gods as far as if this will work or not.
Thanks again for the help.
"But the long and the short of it is that its a bug in Access that doesn't allow the .Text property to be referenced even if there is focus on the object"
Where did you find this ?
mx
Where did you find this ?
mx
ASKER
Wow, sorry that was really poorly written...lets try that again.
Websites:
http://www.eggheadcafe.com/conversation.aspx?messageid=31619464&threadid=31619460
http://allenbrowne.com/bug-06.html
This seems to be the same problem that I was having. What that really should say is IF the object is in the form header then Access can (and will) get confused about using the .text property and gives runtime error 2185.
Sorry about the confusion....and maybe I'm just blowing smoke right now, but it should at least provide some information about the problem.
Just trying to provide some help even though I'm no expert.
Websites:
http://www.eggheadcafe.com/conversation.aspx?messageid=31619464&threadid=31619460
http://allenbrowne.com/bug-06.html
This seems to be the same problem that I was having. What that really should say is IF the object is in the form header then Access can (and will) get confused about using the .text property and gives runtime error 2185.
Sorry about the confusion....and maybe I'm just blowing smoke right now, but it should at least provide some information about the problem.
Just trying to provide some help even though I'm no expert.
ok ... thx for that link. In fact, some strange things *can* happen in the Header of a form - just ran into one yesterday, so that is not surprising.
Hit me with the link again to your other Q ...
mx
Hit me with the link again to your other Q ...
mx
ASKER
https://www.experts-exchange.com/questions/24531280/How-to-create-a-dynamic-filter.html?anchorAnswerId=24755920#a24755920
better set aside some time for reading....I think we're in the neighborhood of 50 posts.
thanks for all the help
better set aside some time for reading....I think we're in the neighborhood of 50 posts.
thanks for all the help
whew. ok ... I will *try* to get to it later.
thx
thx
well ... pDog is a good guy and I'm sure he will get a resolution for you.
mx
mx
ASKER
yeah...i'm sure he will thanks for takin a look.
Me.YourTextbox
SetFocus should work everytime, unless you're not referring to the control correctly (eg: referring to a control on a subform without the correct syntax).
OnChange fires EVERY time the user strokes a key ... is that what you want?