Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Reordering Records with VBA
Edmund Squires 
    
2 years ago
Hello,
I need to fix the order in which Recordset.MoveNext iterates through the records, but I cannot figure out how to manipulate such a thing through VBA. Every attempt I have made starts with the 9th record that is displayed visually. I have used a query to copy the data to another table with a set sort order using an "ID" field.  I have also attempted to iterate through an already sorted recordset and copied each field into a new recordset.  Both failed to accomplish what I would like them to do.  Any suggestions are much appreciated.
Thomas Gonder  @Reply  
      
2 years ago
What are you using to get the recordset source?
What do you mean by "displayed visually"?
Adam Schwanz  @Reply  
           
2 years ago
By Default your move next is going to go to the next record in the table, if it's not in that order, you have an Order By setting on. If you had some strange reason to jump from 3 to 6 to 9 or something as "normal", you'd probably have to use one or a combination of .absoluteposition and .move to a specific record number, but I can't think of any reason you would that you wouldn't just use a filter.

Can you explain more?
Edmund Squires OP  @Reply  
    
2 years ago
(Sorry for the long post, but I hope that explaining a part of my process might give more context to the issue.)

By "displayed visually" I mean when I open the table in the GUI.  I have set the OrderBy and OrderByOn programmatically, as well.
I start with an imported recordset from Excel, then using a subroutine, I extract and transpose some data into a new tableDef.  This is where the order gets wonky. This new table has an ID autonumber field that I would expect is populated in order of added. The table also has a RowID field and Ordinal field (both used to track the inherited data in different ways) which I used to sort the data in the order that I would like. In an attempt to clarify, the original table has one field for each ordinal per record (e.g. "RowID" = 1 would have fields, "Thing1" = 3, "Thing2" = 1, "Thing3" = 2, "Thing4" = 4) which I then transpose and create records for each unique RowID/Ordinal combination (e.g. First Record: "RowID" = 1 "Ordinal" = 1; Second Record: "RowID" = 1 "Ordinal" = 2; etc.)
I have used a query that has the data sorted by RowID and Ordinal when it appends it to a new table.  I have also used OrderBy to set the sort order.  Both work in the GUI, but when I iterated through the records and move it to another table, it is out of order.
Edmund Squires OP  @Reply  
    
2 years ago
I am trying to build a visio drawing using VBA, which is much easier if I can iterate through records in a reliable order.
Thomas Gonder  @Reply  
      
2 years ago
I don't know if this helps, but I have seen in both tables and queries, unless a sort by is used for the autonumber, that the rows won't always sort in order. In other words, using the datasheet for the table without sorts, often shows (for example) record with ID 103 before record with ID 1. I presume it's something deep in Access, like old DOS that puts things in an order based upon some unknown hash or space availability. You can also see the reverse of this when burning CDs in Windows when sometimes you want to burn things (like your music "playlist") in the order that you add them to a folder. In both cases, what you see isn't what you expect because of the internal file structure of directories/tables and their files/records.
Richard Rost  @Reply  
          
2 years ago
Unless you specify what field you want to sort by, you're going to get whatever order they happen to come in, and you can't always rely on autonumbers or datetime stamps.
Kevin Yip  @Reply  
     
2 years ago
Hi Edmund, you set the sort order in a recordset when you open the recordset in VBA:

     Dim r As Recordset
     Set r = CurrentDb.OpenRecordset("SELECT * FROM Table1 ORDER BY ... ;")

Or you can open a query that has a built-in sort order:

     Set r = CurrentDb.OpenRecordset("Query1")

You can also override the query's sort order:

     Set r = CurrentDb.OpenRecordset("SELECT * FROM Query1 ORDER BY ... ;")
Edmund Squires OP  @Reply  
    
2 years ago
Thank you all for your replies.  I don't know exactly what changed, but the query works now.
Here is the SQL of my query:

INSERT INTO CFG1_StressedT_Reduced_Sorted
SELECT CFG1_StressedT_Reduced.*
FROM CFG1_StressedT_Reduced
ORDER BY CFG1_StressedT_Reduced.RowID, CFG1_StressedT_Reduced.Ordinal;

I must have been doing something wrong before.  I appreciate all of the responses.
Richard Rost  @Reply  
          
2 years ago
Gremlins
Thomas Gonder  @Reply  
      
2 years ago
It's gotten to the point where I have to decompile about five times a day to scare the gremlins away.
Thank goodness for that video, or I would be stuck with untold gremlins that just hang on and on.
Search Compile (or Decompile a Microsoft Access Database) here in 599cd.com

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 7:09:51 AM. PLT: 0s