Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a programmatically created MSFLEXGRID by double clicking on its header columns

VBA How To

Sorting a programmatically created MSFLEXGRID by double clicking on its header columns

by  taupirho  Posted    (Edited  )




Here's how. Put these in the general declarations section of a module

Private m_iSortCol As Integer
Private m_iSortType As Integer

Private WithEvents msflexgrid2 As MSFlexGrid


Now onto the module itself


Private Sub msflexgrid2_dblclick()
On Error GoTo err_handler
'-------------------------------------------------------------------------------------------
' code in grid's DblClick event enables column sorting
'-------------------------------------------------------------------------------------------

Dim i As Integer

' sort only when a fixed row is clicked
If msflexgrid2.MouseRow >= msflexgrid2.FixedRows Then
Exit Sub
End If

i = m_iSortCol ' save old column
m_iSortCol = msflexgrid2.MouseCol ' set new column

' increment sort type

If i <> m_iSortCol Then
' if clicking on a new column, start with ascending sort
m_iSortType = 1
Else
' if clicking on the same column, toggle between ascending and descending sort
m_iSortType = m_iSortType + 1
If m_iSortType = 3 Then m_iSortType = 1
End If


With msflexgrid2
.Redraw = False
.Row = 1
.RowSel = .Rows - 1
.col = m_iSortCol
.Sort = m_iSortType
.Redraw = True
End With

endit:
Exit Sub

err_handler:
MsgBox _
"An unexpected error has been detected" & Chr(13) & _
"Description is: " & Err.Number & " , " & Err.Description & Chr(13) & _
"Module is: userform5.msflexgrid2_dblclick" & Chr(13) & _
"Please note the above details before contacting support", , " Unexpected Error"
Resume endit

End Sub

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top