View Full Version : Build Table form Query Out of Order


dmmckelv
12-11-2008, 03:33 AM
Hello,
I used VBA to create a table based on the data in a query. The SQL statement includes ORDER BY Start Time. The VBA is similiar to:
[code]
Dim db As DAO.Database
Dim work as TableDef
Dim all the fields As Fields

Set db =Currentdb
Set work = db.CreateTableDef("workTable")
Set "all the fields" = work.CreateField("blah", db"")

Append All the fields
Append the db
Set Work = db.OpenRecordset("workTable", dbOpenDynaSet)
Set WorkQuery = db.OpenRecordset("workQuery", dbOpenDynaSet)

If Not WorkQuery.EOF Then
WorkQuery.MoveFirst
End If

Do While Not WorkQuery.EOF
Work.AddNew
Work("blah").Value = WorkQuery!blah
etc for all the fiels
WorkQuery.MoveNext
Loop [/ code]

When I open the table, it is in a completely different order. Any ideas why? Is there a way to quickly re-order the table using VBA or even better to prevent this from happening.

Thanks for your help!

Mark

namliam
12-11-2008, 03:41 AM
Records in a table by their nature without a sort are in RANDOM order though 99% of the time seem to be in order (yay! You hit the Jackpot).
Only a specific sort can guarantee the right order.

Why create the table using this code, and why not use a "Create table query" ??
Faster to execute and less work to actually make.

dmmckelv
12-11-2008, 04:20 AM
I implemented the Create Table Query but, the table still seems to be out of order. Does anyone know a procedure for implementing sort without stepping through each row?

namliam
12-11-2008, 04:59 AM
Records in a table by their nature without a sort are in RANDOM order though 99% of the time seem to be in order (yay! You hit the Jackpot).
Only a specific sort can guarantee the right order.


Same question, same answer.... Only a sort will ensure the right order, opening the table doesnt guarantee any order...

dmmckelv
12-11-2008, 06:24 AM
Is there a way to quickly re-order the table using VBA?

namliam
12-11-2008, 06:49 AM
You can just order the table ...

In VBA something like
Currentdb.Tabledefs("YourTablename")
Will allow you to "mess around" with the table, Not sure if you can set a default sorting order. Sorting is something you should do in a query anyway.... base upon that table.