?
Solved

Sorting an Access table using VBA

Posted on 2012-03-31
4
Medium Priority
?
4,123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 37791996
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 1500 total points
ID: 37791999
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
ID: 37792058
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
ID: 37792064
See my comments following those of capricorn1.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

777 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