473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Why won't my OnChange event work?

Seth Schrock
2,965 Expert 2GB
I have a textbox that I'm using as a search field: txtAddress. I have a query that is based on that field using the Like keyword so that I can have wildcards. What I would like to happen is that as I type each character into txtAddress, the subform (that is based on the query) requeries. Here is what I currently have:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtAddress_Change()
  2. If Me.Dirty = True Then Me.Dirty = False
  3. Me.sfrmAddressSearchResults.Requery
  4.  
  5. End Sub
It seems like it is always one change behind. At first I didn't have the 2nd line and nothing happened. That is when I figured out that the value of the field hadn't changed for the query to see it yet, so I had it save and then requery. Now it will change, but if I start to enter 11, and then backspace and then type 80, then it still shows the addresses that starts with 11. If I manually run the query after making the change to txtAddress and then leave the field, the query runs exactly as expected so I know it is in my OnChange event that I'm messing up.
Nov 30 '12 #1

✓ answered by TheSmileyCoder

I presume as you mention txtAddress is used as a search field that it is unbound. As such I don't think it should matter saving the record.

When you reference a control whether by code or query, you will usually reference the VALUE of the control.
Expand|Select|Wrap|Line Numbers
  1. Forms!frm_Example!txtAddress.Value
Now the default property of a textbox is the value, therefore it is possible to write:
Expand|Select|Wrap|Line Numbers
  1. Forms!frm_Example!txtAddress
and it will be equivalent to the line written before.

HOWEVER (There had to be one) the OnChange event is special. The OnChange event is fired whenever you make a change to the TEXT in the textbox. This is different from the BeforeUpdate which fires before the VALUE of the textbox is changed. As you type the TEXT is changed, when you hit enter (or tab or click away) the VALUE is changed. So while you are still typing the value of the control remains unchanged. So if you want something to work with the OnChange event you need to reference the .TEXT property of the control:
Expand|Select|Wrap|Line Numbers
  1. Forms!frm_Example!txtAddress.TEXT
  2. 'or
  3. Me.txtAddress.Text
A caveat is that I believe that you can only use the .TEXT if the control has focus. What I would do would look something like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtAddress_Change()
  2.   dim strFilter as String
  3.   strFilter="AddressFieldName like '*" & me.txtAddress.TEXT & "*'"
  4.   Me.sfrmAddressSearchResults.Form.Filter=strFilter
  5.   If not me.sfrmAddressSearchResults.Form.Filteron then
  6.     Me.sfrmAddressSearchResults.Form.FilterOn=True
  7.   End If     
  8. End Sub
  9.  

11 22573
NeoPa
32,556 Expert Mod 16PB
Me refers to the form rather than the particular field Seth.

.Text & .Value properties should, between them, provide what you're after. I'm unsure which is the more useful at this stage, but testing will determine that.
Dec 1 '12 #2
Seth Schrock
2,965 Expert 2GB
I thought that doing the Me.dirty = false saved the record. I'm not familiar with the .Text or .Value (I assume you meant .Value not .Vaue) properties, so do I do Me.Text.Dirty = False or Me.txtAddress.Text.dirty = False?
Dec 1 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
I presume as you mention txtAddress is used as a search field that it is unbound. As such I don't think it should matter saving the record.

When you reference a control whether by code or query, you will usually reference the VALUE of the control.
Expand|Select|Wrap|Line Numbers
  1. Forms!frm_Example!txtAddress.Value
Now the default property of a textbox is the value, therefore it is possible to write:
Expand|Select|Wrap|Line Numbers
  1. Forms!frm_Example!txtAddress
and it will be equivalent to the line written before.

HOWEVER (There had to be one) the OnChange event is special. The OnChange event is fired whenever you make a change to the TEXT in the textbox. This is different from the BeforeUpdate which fires before the VALUE of the textbox is changed. As you type the TEXT is changed, when you hit enter (or tab or click away) the VALUE is changed. So while you are still typing the value of the control remains unchanged. So if you want something to work with the OnChange event you need to reference the .TEXT property of the control:
Expand|Select|Wrap|Line Numbers
  1. Forms!frm_Example!txtAddress.TEXT
  2. 'or
  3. Me.txtAddress.Text
A caveat is that I believe that you can only use the .TEXT if the control has focus. What I would do would look something like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtAddress_Change()
  2.   dim strFilter as String
  3.   strFilter="AddressFieldName like '*" & me.txtAddress.TEXT & "*'"
  4.   Me.sfrmAddressSearchResults.Form.Filter=strFilter
  5.   If not me.sfrmAddressSearchResults.Form.Filteron then
  6.     Me.sfrmAddressSearchResults.Form.FilterOn=True
  7.   End If     
  8. End Sub
  9.  
Dec 1 '12 #4
NeoPa
32,556 Expert Mod 16PB
Seth:
I'm not familiar with the .Text or .Value (I assume you meant .Value not .Vaue) properties, ...
Absolutely. My bad. I've fixed the typos now.
Seth:
I thought that doing the Me.dirty = false saved the record, so do I do Me.Text.Dirty = False or Me.txtAddress.Text.dirty = False?
No. That was the track I was trying to lead you away from.

I would have hoped that you would have looked at the .Text property after my last post. If you were to, you would discover that it reflects the typing immediately (Within the _Change() event procedure), and can be used as a filtering value.
Dec 1 '12 #5
Seth Schrock
2,965 Expert 2GB
I have read the TextBox.Text property from MSDN, but I'm still not positive that I know where you are going, but I do have a guess (Smiley's post helped). So instead of having the query have txtAddress as it criteria, I should use filters on the form via VBA. This would allow me to use the Me.txtAddress.text property as the filter criteria in the OnChange event just like Smiley has posted.
Dec 1 '12 #6
NeoPa
32,556 Expert Mod 16PB
Sometimes it's easier to try something out rather than looking it up in Help or the web Seth. That's what I did. I knocked up a very quick procedure on an existing TextBox control on one of my forms and noticed the results :
Expand|Select|Wrap|Line Numbers
  1. Private Sub XXX_Change()
  2.     Debug.Print Me.XXX.Value, Me.XXX.Text
  3. End Sub
I typed the characters "ABcd" into the control ([XXX]) and saw (in the Immediate Pane) :
Expand|Select|Wrap|Line Numbers
  1. Null    A
  2. Null    AB
  3. Null    ABc
  4. Null    ABcd
From there it's a short step to the workable solution you're talking about. Very similar to Smiley's suggestion as you've noticed. Your original post indicated to me you didn't need the rest of it explained as you already had that part understood well enough (That was my reading of it at least).

PS. BTW, your original approach of saving the record would have been relatively pointless, except in that it probably forced the control to consider itself as having been updated. IE. Similar to tabbing out of the control. You need neither of those now you understand how to use the .Text property.
Dec 1 '12 #7
Seth Schrock
2,965 Expert 2GB
I will give it a try when I get to work on Monday. Thanks.
Dec 1 '12 #8
Seth Schrock
2,965 Expert 2GB
That worked. I actually have four different fields for search fields (txtAddress, txtCity, txtState, txtZIP) so I had to make a few additions to the code, but I did get it to work. Please let me know what you think of my final code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub FormFilter()
  2. Dim strFilter As String
  3.  
  4. Me.txtAddress.SetFocus
  5. strFilter = "PropAddress like '" & Me.txtAddress.Text & "*'"
  6.  
  7. Me.txtCity.SetFocus
  8. strFilter = strFilter & "AND PropCity Like '" & Me.txtCity.Text & "*'"
  9.  
  10. Me.txtState.SetFocus
  11. strFilter = strFilter & "AND PropState Like '" & Me.txtState.Text & "*'"
  12.  
  13. Me.txtZIP.SetFocus
  14. strFilter = strFilter & "AND PropZIP Like '" & Me.txtZIP.Text & "*'"
  15.  
  16.  
  17. Me.sfrmAddressSearchResults.Form.Filter = strFilter
  18.  
  19. If Not Me.sfrmAddressSearchResults.Form.FilterOn Then
  20.     Me.sfrmAddressSearchResults.Form.FilterOn = True
  21. End If
  22.  
  23. End Sub
txtAddress OnChange event (all the others are the same, but reference themselves):
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtAddress_Change()
  2. FormFilter
  3.  
  4. Me.txtAddress.SetFocus
  5. With Me.txtAddress
  6.     .SelStart = Len(.Text)
  7.  
  8. End With
  9. End Sub
Dec 3 '12 #9
NeoPa
32,556 Expert Mod 16PB
Seems the bee's knees Seth. One point I would make is that line #4 of the OnChange event would be better placed after, and to take advantage of, the With statement in line #5.
Dec 3 '12 #10
Seth Schrock
2,965 Expert 2GB
Duh... That is so simple even I should have seen that. Oh well.
Dec 3 '12 #11
NeoPa
32,556 Expert Mod 16PB
I'm sure you would have, given time to allow your concentration to recede from the main issue of getting it to work at all. It's not uncommon to miss details like that while concentrating on just getting it to work.
Dec 3 '12 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Matt | last post by:
I have radio buttons on a form, and when the user makes the selection, it will trigger the event and send the form to the web server and print the selected value. In Case 1, I just use submit...
4
by: Barry G. Sumpter | last post by:
Complete newbie here. Just sorted out that myASP.asp will ONLY execute on c:\inetput\wwwroot when I access it thru http://localhost/myASP.asp but if I copy myASP.asp to a sub folder of...
2
by: Robbie | last post by:
Hi, I want to programatically fire an event. I have it working in IE, but Netscape won't work. What is the Netscape equivalent to, or a workaround for: document.myForm.myField.onchange(); ...
2
by: Matt | last post by:
The following code won't work, because onchange event is not trigger when drop down size = 1. But if I make the size="2" or greater, then it will work. Is that true? please advise. thanks!! ...
3
by: dthmtlgod | last post by:
When a user changes the value in a select statement, I would like to update the recordset using a onchange event. Could someone please point me in the right direction? Would like to stay away...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
3
by: JR | last post by:
I know this is probably dumb but I have a web combo box that will drop down and I can select an item but SelectedIndexChanged doesn't fire. I tried to put the code in the ASP that says on the...
1
by: daokfella | last post by:
I need to raise the onchange event and Firefox is giving me some grief. I hear fireEvent is buggy with some events. Is that true? My code is simply this: targetTextbox.fireEvent("onChange"); ...
2
by: Nathan Sokalski | last post by:
I have the following code which allows you to drag a div in IE, and have it then move back to it's natural position when you release the mouse button: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML...
13
by: andypb123 | last post by:
Hello, The onchange event fires in IE6 in a SELECT element when scrolling through the list with the up and down arrows on the keyboard. In Firefox it only fires after you hit the enter key, which...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.