Solved

MS ACCESS - SORT BY COLUMN HEADING ON CLICK

Posted on 2010-09-06
8
918 Views
Last Modified: 2012-05-10
I've created some forms in tabular format.  I would like to be able to click on different column headings and sort by the column clicked in alpha numeric order.
0
Comment
Question by:wawaldo
[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
8 Comments
 
LVL 2

Expert Comment

by:ngmarowa
ID: 33610585
Try this:

Right click the column you want to click. Goto properties and the event. You can define a procedure to sort the under "On Click"
0
 
LVL 33

Expert Comment

by:jppinto
ID: 33610588
You have to display your forms in datasheet view. Then right click the column heading, and the menu will include sort options.

jppinto
0
 

Author Comment

by:wawaldo
ID: 33610621
nqmarowa thats is exactly what i would like to achieve.  I just do not remeber the VBA code. something along the lines of
SELECT ------    SORT ---

Used it a couple of years ago, but lost it since then.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 2

Accepted Solution

by:
ngmarowa earned 500 total points
ID: 33610730
SELECT *
FROM <Tablename>
ORDER BY <tablename.Field>;
0
 
LVL 6

Expert Comment

by:iandian
ID: 33610774
You can use the OrderBy property of a form to set which column should be used to order the data.
Don't forget to set the OrderByOn property on your form to true.

I attached some cude you can use in a combination with a click event (e.g. on the label of you columns) to order the data. (Found it somewhere, credit to whoever wrote it, just can't remember)
Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
'Purpose: Set a form's OrderBy to the string. Reverse if already set.
'Return: True if success.
'Usage: Command button above a column in a continuous form:
' Call SortForm(Me, "MyField")
    If Len(sOrderBy) > 0 Then
        ' Reverse the order if already sorted this way.
        If frm.OrderByOn And (frm.OrderBy = sOrderBy) Then
            sOrderBy = sOrderBy & " DESC"
        End If
        frm.OrderBy = sOrderBy
        frm.OrderByOn = True
        ' Succeeded.
        SortForm = True
    End If
End Function

Open in new window

0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 33610801
Actually, all you need to do is use the OrderBy property of the form.  As an example, assume that one of your columns is [TestDate], and the label that heads that column is named lbl_TestDate.  In that case, open the form in design view, select the lable for that field.  Then, in the property window, select the Event tab and set the On Click event to [Event Procedure], then click the elipse button to expand the VB editor window.

Once in the VB IDE, copy the code below and paste it into the click event.  You will need to do this for every column in your form, ensuring that you change the the name of the field (TestDate) for each column.

Private Sub lbl_TestDate_Click()

    If InStr(Me.lbl_TestDate.Tag, "DESC") = 0 Then
        Me.OrderBy = "TestDate Desc"
        me.lbl_TestDate.Tag = "Desc"
    Else
        Me.OrderBy = "TestDate"
        me.lbl_TestDate.Tag = ""
    End If
    Me.OrderByOn = True
   
End Sub

The purpose of the lines that test or manipulate the lbl_TestDate.Tag property is to ensure that each column has its own sort "memory".
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 33611734
Since sorting columns is a built-in property of Access datasheets, I usually just put a note to that effect in a main form with a datasheet subform, as in the screen shot below:
Fancy-Filters-Form.jpg
0
 
LVL 75
ID: 33612661
wawaldo
How does the Accepted Answer ... answer this question ?  It has nothing to do with columns per se.  The simplest approach is @ http:#a33611734

mx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

707 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