Question

I already have a VBA script that edits this, that, and the other and even opens the table at the end of the script. The only problem is that I want the data I am viewing to be sorted by columnA, and then by columnB.

How do I do that via VBA in Access 2010?

Was it helpful?

Solution

If you just want to see the record set then you could do something like the following:

Dim qdef As Dao.QueryDef
Set qdef = CurrentDb.CreateQueryDef("MyQuery", "SELECT * " & _
                                               "FROM TableName " & _
                                               "ORDER BY columnA, columnB")
DoCmd.OpenQuery "MyQuery"

Then once you are done doing whatever it is you want to do with it you could execute the following to remove the query:

On Error Resume Next
DoCmd.DeleteObject acQuery, "MyQuery"

Or you could do the following:

Dim RSData as DAO.Recordset
Set RSData = CurrentDB.OpenRecordSet("SELECT * " & _
                                     "FROM TableName " & _
                                     "ORDER BY columnA, columnB")
If (RSData.RecordCount > 0) Then
  RSData.MoveFirst
  Do While RSData.EOF <> True
    'HERE YOU CAN WORK WITH EACH FIELD OF THE RECORD INDIVIDUALLY USING 
    RSData.Fields("columnA").value

    RSData.MoveNext
  Loop
End If

Then once you are done doing whatever it is you want to do with it you could execute the following:

RSData.Close
Set RSData = Nothing
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top