473,396 Members | 1,879 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,396 software developers and data experts.

Why won't my "requery" command sort the results?

Seth Schrock
2,965 Expert 2GB
I have a form (frmEmployee) with a field called SystemID. I also have a query that finds all of the system IDs currently used and sorts them in numerical order. This query is the basis for a subform on form frmEmployee. The purpose of this subform is to be able to see what system IDs are being used so that a new system ID can be used when entering a new employee. In the SystemID field, I have an AfterUpdate event that requeries the subform so that it registers the new system ID or a changed system ID. My problem is that if I change a system ID from 20 to a non-existent 13, then the subform lists it as 12, 14, 15, 16, 17, 18, 19, 13, 21. If I then change it back to the original 20, then it shows 12, 20, 14, 15, 16, 17, 18, 19, 21. If I click on the refresh records button in the ribbon, then the sort works. Why doesn't the sort from the query work when the subform is requeried?
Aug 8 '11 #1

✓ answered by NeoPa

Ok Seth, think about exactly what has happened at the point of the SystemID_AfterUpdate(). Data is changed on the form in that control. The important point here is that nothing has changed in the table at this point. The form simply reflects a buffer of potential changes that have not yet been committed. Trying to check the table now would make sense only if the logic were wrong.

As for setting the flag in SystemID_AfterUpdate(), that just means that the code in Form_AfterUpdate() could be changed to run the .Requery code only when the changes on the form included a change to SystemID.

See below an example of what I'm trying to explain :

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private blnIDChanged As Boolean
  5.  
  6. Private Sub Form_AfterUpdate()
  7.     If blnIDChanged Then
  8.         Me.{YourSubformControlNameHere}!sfrmUsedSystemIDs.Requery
  9.         blnIDChanged = False
  10.     End If
  11. End Sub
  12.  
  13. Private Sub Form_Current()
  14.     blnIDChanged = False
  15. End Sub
  16.  
  17. Private Sub SystemID_AfterUpdate()
  18.     blnIDChanged = True
  19. End Sub
I hope that's a lot clearer now, but feel free to ask for further clarification if anything is still unclear.

15 3460
NeoPa
32,556 Expert Mod 16PB
It looks like what you have works, but you're doing it in the wrong place. It's hard to say more without more info, but that should tell you where to look at least.
Aug 8 '11 #2
Seth Schrock
2,965 Expert 2GB
What is in the wrong place? My requery code?
Aug 8 '11 #3
NeoPa
32,556 Expert Mod 16PB
I'm guessing so. There's so little info here it's hard to be sure, but the ordering of the results you've displayed is a bit of a clue.
Aug 8 '11 #4
Mihail
759 512MB
Try to refresh your form from VBA: FormName.Refresh (or Me.Refresh)
Aug 9 '11 #5
NeoPa
32,556 Expert Mod 16PB
That won't help I'm afraid Mihail. Seth is already doing a .Requery, and that is a superset of .Refresh. IE. It does all that .Refresh does and more.
Aug 9 '11 #6
Mihail
759 512MB
Thank you, NeoPa. It is useeful to know.
Seth say that the Refresh button from the ribbon do the job. And he press this button, of course, AFTER he run the code. I think in terms cause-effect (not neccessary true).
Aug 9 '11 #7
Seth Schrock
2,965 Expert 2GB
I did try to do a .refresh command right after the .requery, but it said that the .refresh was not valid for the form. Maybe because it is based on a query.

On frmEmployee, I have two subforms: sfrmFobRegistration and sfrmUsedSystemIDs. The field SystemID is in sfrmFobRegistration. Currently, I have the .requery command in the AfterUpdate event of the sfrmFobRegistration.SystemID field. I just ran a test, and when I comment out the .requery command the result is the same as when I include the .requery command, except that when the .requery command is used, the form goes back to the first record. So it is as if the form is refreshing without running the query that is behind it, if that is possible. Here is the exact code that I'm using for the .requery command:
Expand|Select|Wrap|Line Numbers
  1. Forms!frmEmployee!sfrmUsedSystemIDs.Requery
I can't think of any other place to put the .requery command other than the AfterUpdate event because the form doesn't need requeried until the information would change which is when the SystemID field is updated. Is there a better way?
Aug 9 '11 #8
NeoPa
32,556 Expert Mod 16PB
If it's in the wrong place Seth, you are going to include the code that handles the AfterUpdate event. The whole event procedure. That way we may have something to go on. I tried to drop a hint earlier about too little information. Perhaps I was too subtle.
Aug 9 '11 #9
Seth Schrock
2,965 Expert 2GB
Sorry, I tried to give you more information, but I didn't know what information you needed. Here is the whole AfterUpdate event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub SystemID_AfterUpdate()
  2.  
  3.     Forms!frmEmployee!sfrmUsedSystemIDs.Requery
  4.  
  5. End Sub
  6.  
Do you need anything else? I'll try to answer any question to the best of my ability, but I can't promise that I will know the answer :)
Aug 9 '11 #10
NeoPa
32,556 Expert Mod 16PB
No Seth. That's plenty.

Put the code in the form's AfetrUpdate procedure. I'm not sure how well this will work, but give it a try (The results you report don't seem logical even when triggered at a time which is essentially correct, but let's see what happens when done in the proper place).

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_AfterUpdate()
  2.     Me.{YourSubformControlNameHere}!sfrmUsedSystemIDs.Requery
  3. End Sub
You may wish to set a flag in the SystemID_AfterUpdate() event procedure to notify the .Requery is required, but it should only be called when the record has actually updated in the table (which doesn't happen until the form is updated of course).
Aug 9 '11 #11
Seth Schrock
2,965 Expert 2GB
Okay, it worked. However, it doesn't do anything until the focus leaves the form. This is the obvious result because that is when the AfterUpdate event occurs. I just think that it would make more sense to only requery the subform when the data would change. In this case, the subform is requeried no matter what field is updated. I do think that it will work for my purpose, however.

What gets me is that it didn't work in the AfterUpdate event of the field. As long as the code is correct (I copied and pasted from what I already had) and the event is triggered, shouldn't the result be the same?

Oh, and what do you mean by setting a flag in the SystemID_AfterUpdate() event procedure to notify the .requery is required? I didn't get that part.
Aug 9 '11 #12
NeoPa
32,556 Expert Mod 16PB
Ok Seth, think about exactly what has happened at the point of the SystemID_AfterUpdate(). Data is changed on the form in that control. The important point here is that nothing has changed in the table at this point. The form simply reflects a buffer of potential changes that have not yet been committed. Trying to check the table now would make sense only if the logic were wrong.

As for setting the flag in SystemID_AfterUpdate(), that just means that the code in Form_AfterUpdate() could be changed to run the .Requery code only when the changes on the form included a change to SystemID.

See below an example of what I'm trying to explain :

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private blnIDChanged As Boolean
  5.  
  6. Private Sub Form_AfterUpdate()
  7.     If blnIDChanged Then
  8.         Me.{YourSubformControlNameHere}!sfrmUsedSystemIDs.Requery
  9.         blnIDChanged = False
  10.     End If
  11. End Sub
  12.  
  13. Private Sub Form_Current()
  14.     blnIDChanged = False
  15. End Sub
  16.  
  17. Private Sub SystemID_AfterUpdate()
  18.     blnIDChanged = True
  19. End Sub
I hope that's a lot clearer now, but feel free to ask for further clarification if anything is still unclear.
Aug 10 '11 #13
Seth Schrock
2,965 Expert 2GB
Okay, that makes sense. I didn't think about the fact that the table wouldn't be updated.

And I got the setting the flag part too. I have never seen this done, but it does make sense. Seeing the code, I figured out what is happening. I've included it in my code. Is there a term for code that start with just a "Private" instead of "Private Sub"? I would like to learn more about it, but I don't know what it would be called to Google it.

Thanks for your help.
Aug 10 '11 #14
NeoPa
32,556 Expert Mod 16PB
Private simply defines the scope of whatever is being declared. Private followed by a simple name (rather than Const; Sub; Function; etc) is a declaration of a variable. If you look up Private you'll see it specifies the scope of the item (whether that is a variable or procedure) which indicates where in your project it is recognised from. In this case the variable is private to that particular module, so it can be seen anywhere in any of the procedures within that module, but not seen by any other modules.
Aug 10 '11 #15
Seth Schrock
2,965 Expert 2GB
Okay. Thanks again.
Aug 10 '11 #16

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

Similar topics

9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
11
by: Jan | last post by:
Hi: Here's a problem I've had for a long time. The client is really running out of patience, and I have no answers. Access2003, front- and back-end. Single form with 4 subforms (each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.