Solved

Sorting an Access table using VBA

Posted on 2012-03-31
4
3,778 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 500 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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

749 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