军事时间表单验证 msAccess 表单视图

Military Time Form Validation msAccess Form View

我很难在表单视图中执行军事时间或短时间。我发现了一个错误,用户输入 15:83 是不允许的,因为分钟数超过 59,小时数超过 23。但是,我不确定如何为类似的东西创建表单验证那而且我似乎无法在网上找到任何关于这种格式的文档。

I can't seem to find any documentation for it online surrounding this type of formatting.

那是因为它很困难并且需要相当多的代码,post 太多了。但是,例如,要更正无效输入:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
  
    Const TimeHourMaximum   As Integer = 24
    Const TimeHourDefault   As Integer = 20
    Const TimeMinuteTenMax  As Integer = 5
    
    Dim ctl                 As Control
    
    Dim Text                As String
    Dim SelStart            As Integer
    
    On Error Resume Next
    
    Set ctl = Screen.ActiveControl
    
    Select Case ctl.Name
        Case "Logon"
            Text = ctl.Text
            SelStart = ctl.SelStart
            If Not IsDate(Text) Then
                DoCmd.Beep
                If Val(Left(Text, 2)) > TimeHourMaximum Then
                    Mid(Text, 1) = CStr(TimeHourDefault)
                ElseIf Len(Text) > 3 Then
                    ' Length of Text is larger than two hour digits and the kolon.
                    Mid(Text, 1 + 3) = CStr(TimeMinuteTenMax)
                End If
            End If
            ctl.Text = Text
            ctl.SelStart = SelStart
            ctl.SelLength = 1
            Response = acDataErrContinue
    End Select

    Set ctl = Nothing

End Sub

完整代码、文档和演示应用程序已与我的项目 VBA.TimeEntry 一起 post 编辑在 GitHub:

Entering 24-hour time with input mask and full validation in Microsoft Access

在表单文本框控件的验证规则属性中输入这个表达式

=右([Text6],2)<60 或左([Text6],2)<24

我假定控件的名称是 Text6