How to: Enter a number with arrows and a unit
Let users set a number within limits, such as a key's lifetime in days or a session length in hours,
with WklSpinEdit on a form or with a spin column in a list.
Requires WKL Forms Object Library 1.43 or later.
Solution overview
- Bind it like a text box: Control Source (
ControlSource) names a number field. - Set the rules:
MinValue,MaxValue,Step,DecimalPlaces, andUnit(the text after the number), either on the designer's Number page or in code in Design view. - Tell the user why an entry was rejected in
InvalidEntry; the control doesn't show a message box. - In a list: use a column with
Editor = wedSpin,MinValue,MaxValue,Step, andUnit.
Implementation
A form frmKey is bound to tblKey and contains a WklSpinEdit spnDays bound to the field
LifetimeDays, plus a label lblHint:
' Once, in design view (or on the designer's Number page); saved with the form.
With Forms!frmKey!spnDays
.ControlSource = "LifetimeDays"
.Object.MinValue = 1
.Object.MaxValue = 3650
.Object.Step = 30
.Object.Unit = "days"
End With
' Module of frmKey: say why an entry was refused, and clear the hint after a valid change.
Private Sub spnDays_InvalidEntry(ByVal Text As String, ByVal Message As String)
Me!lblHint.Caption = """" & Text & """: " & Message
End Sub
Private Sub spnDays_Change()
Me!lblHint.Caption = ""
End Sub
The same rules for a column in a WklGrid named Grid1:
With Me!Grid1.Object.Columns("LifetimeDays")
.Editor = wedSpin
.MinValue = 1
.MaxValue = 3650
.Step = 30
.Unit = "days"
End With
Remarks
- Decimal places. On a form,
DecimalPlacesrounds and displays the number;Valueis then a Double. In a column, the number of decimal places follows the step (a step of 0.25 shows two). - Typing. A valid number is reported to Access while the user types. Intermediate values on the way to a rejected number (400 on the way to 4000 with
MaxValue = 3650) are reverted when the user leaves the field. - Values outside the limits that come from the record or from code are displayed unchanged.
- Locked and AllowEdits. A locked control or a form with
AllowEdits = Falseignores the arrows, keys, and typing. - Not in the detail section of a continuous form: Access creates only one instance of the control there; in a list, use a spin column in
WklGridinstead.
Related topics
en/wiki/msaccess/comctl/how-to/enter-a-number-with-arrows.txt · Last modified: by Wolfgang van der Stille