Solved

Sorting an Access table using VBA

Posted on 2012-03-31
4
3,367 Views
Last Modified: 2012-03-31
I'm looking for a simple line or two of code to sort an Access able by a specific field.  Thus far I have tried the following and some variations of it but without success.  I want to sort the field WtdAvgPctGoalMet in descending order.

Dim strSQL As String
    strSQL = "ALTER TABLE [table name] SORT COLUMN WtdAvgPctGoalMet Desc"
    CurrentDb.Execute strSQL

The above produces a syntax error message but gives no clue as to what should be changed.  Any suggestions would be appreciated.
0
Comment
Question by:Liberty4all
  • 2
4 Comments
 
LVL 29

Expert Comment

by:IrogSinta
Comment Utility
What is your purpose of sorting the data in the actual table when the records can be viewed in a sorted order?  On a form, you could either update the RecordSource property (RowSource property if a listbox) to include the Order By keyword (ie. SELECT [table name] ORDER BY WtdAvgPctGoalMet Desc), or you could just use the OrderBy property on the form itself.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
try this codes, you have to create a query to do this

Dim strSQL As String, qd As DAO.QueryDef
If DCount("*", "msysobjects", "name='q1'") = 0 Then
   'create the query definition
    strSQL = "select * from TABLENAME order by  WtdAvgPctGoalMet Desc"
    Set qd = CurrentDb.CreateQueryDef("q1", strSQL)
End If
'open the query
   DoCmd.OpenQuery "q1"
0
 

Author Comment

by:Liberty4all
Comment Utility
My reason for sorting records in the table is to do this prior to using code to add and populate a field that ranks them using the following code.

strSQL = "ALTER TABLE [table name] ADD COLUMN Rank COUNTER"
    CurrentDb.Execute strSQL

The table is populated from two different sources.  This means the values in it are not sorted in descending order which is necessary before running the code to add and populate the Rank field.  I was hoping to use code to perform the sort function first but can obviously do it by adding another query to the mix.  

I will accept the solution offerred by capricorn1 for future use.
0
 

Author Closing Comment

by:Liberty4all
Comment Utility
See my comments following those of capricorn1.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now