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.
Who is Participating?
ngmarowaConnect With a Mentor Commented:
FROM <Tablename>
ORDER BY <tablename.Field>;
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"
You have to display your forms in datasheet view. Then right click the column heading, and the menu will include sort options.

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

wawaldoAuthor Commented:
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.
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

Dale FyeCommented:
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"
        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".
Helen FeddemaCommented:
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:
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
How does the Accepted Answer ... answer this question ?  It has nothing to do with columns per se.  The simplest approach is @ http:#a33611734

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.