How to: Switch a view with segments
Offer the periods „Day,“ „Week,“ and „Month“ side by side on a list form, like the view switchers in Office, and filter the form's records by the chosen period.
Requires WKL Forms Object Library 1.27 or later.
Solution overview
A WklButton of kind wbkSegment shows its segments (AddSegment) side by side; one of them is
selected (SelectedKey), and the user's choice raises Change. The segments and the selection you
set in Design view are saved with the form, so no code is needed to set them up; Change only
applies the filter.
Implementation
In the designer ([...] in the Custom row), set the kind to Segments, the caption to
„Period“ (the name screen readers announce), and on the Segments page enter the rows day /
Day, week / Week, and month / Month, with Chosen set for day. Then add this code:
Private Sub Form_Load()
ApplyPeriod Me!btnPeriod.Object.SelectedKey
End Sub
Private Sub btnPeriod_Change()
ApplyPeriod Me!btnPeriod.Object.SelectedKey
End Sub
Private Sub ApplyPeriod(ByVal period As String)
Select Case period
Case "day": Me.Filter = "OrderDate = Date()"
Case "week": Me.Filter = "OrderDate >= Date() - Weekday(Date(), 2) + 1"
Case "month": Me.Filter = "Year(OrderDate) = Year(Date()) And Month(OrderDate) = Month(Date())"
End Select
Me.FilterOn = True
End Sub
Remarks
Changedoesn't occur when code setsSelectedKey; that's whyForm_Loadapplies the filter itself.- Clicking the selected segment changes nothing, just as in an option group. With
MultiSelect, each segment toggles on and off; readSelectedKeysin that case. - The keyboard reaches the segments with a single tab stop; the Left Arrow and Right Arrow keys select the neighboring segment.
- Disable a segment with
SegmentEnabled; the keyboard skips it.