Hi everyone, I have a sub that works fine when used with the lost focus event, but when I try and use the same sub with the change event I get the above error.
I've searched the net and found lots of information but none of it that I can use.
I'm sure I'm missing something basic so apologies if it's a schoolboy error!
This all applies to an Acess 2003 form. I'm looking to create an instant search that checks a number entered into a text box against a table to return a name. It works fine when the lost focus event is used but this means the user has to click off to see the outcome of the search. As the Dlookup I'm using is instant it won't slow down the form to run the sub every time there is a key press on this text box.
I'm missing something basic though because just changing the event to change is throwing out syntax errors when previously there weren't any.
Here's the code as it is, thanks for any help: - Private Sub TxtProxyUser_Change()
-
-
OnlyNumbers
-
-
If IsNull(Me.TxtProxyUser) Then
-
Me.TxtProxyName = Null
-
Else
-
End If
-
-
If Len(Me.TxtProxyUser.Text) > 0 Then
-
Me.TxtProxyName.Value = Nz(DLookup("Consultant", "TblStaffList", "[Personnel Number]=" & Me.TxtProxyUser), "Not Found")
-
Else
-
End If
-
-
End Sub
The reason your code fails when you move it to another event is because:
Not all properties are equally available within the 2 events.
In BeforeUpdate or AfterUpdate you can use ControlName.Value just fine. You can also use ControlName.Value to reference any other control besides teh currently active one. The .Value is also the default property of most controls, and therefore you can often omit the .Value, and the code will still run fine.
Now .Text on the otherhand will sometimes LOOK similar to .Value but they are NOT the same (well duh)
For example you can only call the .Text on a control that has focus. The value stored in .Text is the UNSAVED, currently IN EDIT version of the text.
In your case, your control is empty when you start, so both .Value and .Text will be empty strings.
Now as you start typing your first alphanumeric charecter (For example "T")into the textbox the change event fires. If you want to try it out, use the following code to better understand what happens: - Private Sub TxtProxyUser_Change()
-
Debug.print ".text: " & me.TxtProxyUser.Text
-
Debug.print ".Value: " & me.TxtProxyUser.Text
-
End Sub
would yield in your immediate pane:
If you now press h, the result would be:
Now in your code: - If Len(Me.TxtProxyUser.Text) > 0 Then
-
Me.TxtProxyName.Value = Nz(DLookup("Consultant", "TblStaffList", "[Personnel Number]=" & Me.TxtProxyUser), "Not Found")
-
Else
-
End If
you are using Me.TxtProxyUser (Which as previously stated will force access to use the default property .Value, which as shown above will be an empty string, when used from the CHANGE event, (while it will be NON-Empty if used from the beforeUpdate or AfterUpdate)
If you want to use it in the change event (Which, if you want the search to update EACH time you press a key, as opposed to when you are done entering, and press enter, is the correct event to use) then you simply need to change your code to use the .Text as shown below: - If Len(Me.TxtProxyUser.Text) > 0 Then
-
Me.TxtProxyName.Value = Nz(DLookup("Consultant", "TblStaffList", "[Personnel Number]=" & Me.TxtProxyUser.Text), "Not Found")
-
Else
-
End If
12 2253
You said you get the above error but you never posted any error code.
Sorry, my browser crashed and I was pasting this back in. I must have missed the error part.
Its runtime 3075. It says there is a syntax error in the query expression '[Personnel Number]='.
It highlights the Dlookup when it debugs.
What does it show as the value in the debugger?
Sorry Rabbit, I'm not following. What do you mean the value? With the above sub headed with an "change" event, when a value is entered into the text box it debugs with the error message I posted then the code window highlights line 11.
I don't know if it'll help but the "only numbers" part of the code calls a sub that ensures it's only a numerical value that can be entered into the text box.
When I'm testing it even if I put a letter into the textbox it debugs in the same way. So it's like the sub crashes before it even calls out, but crashes on a later line of code.
I always compile my forms after any changes to check for errors but that doesn't find this, its only when the form is running it happens.
Actually, after running the code in break mode the only number sub seems to have stopped working entirely. It's just skipping over values that aren't numbers so that's a different issue I need to work through. As it's a different sub then I don't see the issues being connected, unless because the event is "change" it affects the values for items in the textbox?
Just an idea:
Before the control lost focus, the update event is raised.
On the other hand, when the _Change event is fired, the update event is not (yet) fired.
It is why (I think) Access ask you for [Personnel Number]
Yes, that makes sense. It seems to fit with what I'm seeing. This just comes from my lack of knowledge in some areas (being self taught).
So if this is the way Acess treats things in the change event, how would you ever get any sub to work in that event? It would always run before it's had a chance for the change in question to take effect.
From my experience: do not use the Change event with bound controls.
Use the After Update event.
Or, if you still wish to use the Change event, force the update event.
Try something like this (but I have doubt about that): - Private Sub Control_Change
-
Update record set
-
......... other code
-
End Sub
cheers Mihail, I'll have a try at that. Using after update crossed my mind briefly, but again I've never used it so was wary. Will that act in the 'instant search' way I'm looking for? That's the main outcome I want. Every key press results in another search being done.
Take a look to post #8 in this thread.
Maybe it is what you are looking for.
The reason your code fails when you move it to another event is because:
Not all properties are equally available within the 2 events.
In BeforeUpdate or AfterUpdate you can use ControlName.Value just fine. You can also use ControlName.Value to reference any other control besides teh currently active one. The .Value is also the default property of most controls, and therefore you can often omit the .Value, and the code will still run fine.
Now .Text on the otherhand will sometimes LOOK similar to .Value but they are NOT the same (well duh)
For example you can only call the .Text on a control that has focus. The value stored in .Text is the UNSAVED, currently IN EDIT version of the text.
In your case, your control is empty when you start, so both .Value and .Text will be empty strings.
Now as you start typing your first alphanumeric charecter (For example "T")into the textbox the change event fires. If you want to try it out, use the following code to better understand what happens: - Private Sub TxtProxyUser_Change()
-
Debug.print ".text: " & me.TxtProxyUser.Text
-
Debug.print ".Value: " & me.TxtProxyUser.Text
-
End Sub
would yield in your immediate pane:
If you now press h, the result would be:
Now in your code: - If Len(Me.TxtProxyUser.Text) > 0 Then
-
Me.TxtProxyName.Value = Nz(DLookup("Consultant", "TblStaffList", "[Personnel Number]=" & Me.TxtProxyUser), "Not Found")
-
Else
-
End If
you are using Me.TxtProxyUser (Which as previously stated will force access to use the default property .Value, which as shown above will be an empty string, when used from the CHANGE event, (while it will be NON-Empty if used from the beforeUpdate or AfterUpdate)
If you want to use it in the change event (Which, if you want the search to update EACH time you press a key, as opposed to when you are done entering, and press enter, is the correct event to use) then you simply need to change your code to use the .Text as shown below: - If Len(Me.TxtProxyUser.Text) > 0 Then
-
Me.TxtProxyName.Value = Nz(DLookup("Consultant", "TblStaffList", "[Personnel Number]=" & Me.TxtProxyUser.Text), "Not Found")
-
Else
-
End If
Thank you so, so much Smiley. I'm sorry it's taken me so long to reply but this is the first of me being back at it from days off.
It worked exactly as I needed it to. Thank you also for not just fixing the problem, but explaining it in a way that means I understand it for future projects.
You rock dude.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andrea Griffini |
last post by:
I did it.
I proposed python as the main language for our next CAD/CAM
software because I think that it has all the potential needed
for it. I'm not sure yet if the decision will get through, but...
|
by: marius |
last post by:
Hi!
I'm currently trying to move from MS SQLServer to MySQL, running on
windows XP.
When trying to connect to mysql on localhost with mysql control center
I get the 2013-error "Lost connection...
|
by: Ryan Park |
last post by:
Hi,
//SITUATION
I got a panel control that hold a certain position on a form.
Every controls or UIs are on this panel.
At certain situation, I called dispose() method of this panel control...
|
by: modemer |
last post by:
I saw someone use the following code:
void func(MyClass *& myCls)
{
myCls->test();
}
// call func():
func(new MyClass);
|
by: Laermans_k |
last post by:
Hi,
Does anyone have a solution to use the <input type="file"
id="filechooser"> and the <input type="submit" id="submitbutton"> in 1
button click?
I've already tried to create a javascript...
|
by: Paul |
last post by:
I have an Access 2000 database with a form that is giving me some
major headaches. When you open the form, it displays all records and
allows editing, but has AllowAdditions set to False so that...
|
by: jbsfe |
last post by:
I have "Spilt" my database and the "lookup" and "seek" methods that
previously worked, no longer do. I have learnd from reviewing the
posts that the "lookup" and "Seek" methods cannot be used on...
|
by: Yannick Turgeon |
last post by:
Hello,
I'm using A97 (french version) on XP (english version).
Since this afternoon, all the Access built-in french-equivalent
function are generating a "Sub or Function not defined" error. An...
|
by: evildracko |
last post by:
Hi, since last time I have still note been able to solve this one, im starting to get frustrated, does anyone know if there is a way to force a page break on a sub report instead of the can grow...
|
by: tpgames |
last post by:
I keep fat fingering the silly "APPLE + ," and getting the preference box up in Firefox. Problem is, that it changed focus and then deletes my home page choice. :P This happens very time I'm on Chat....
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |