html 输入掩码密码_在Microsoft Access中输入带有输入掩码和完全验证的24小时时间

html 输入掩码密码

输入时间很困难 (To enter time is difficult)

Entering time in a form in Microsoft Access can be difficult. There is no time picker like the date picker, and if there was, it would be too slow to operate for many users. Even worse, validation is only rudimentary, meaning that errors are often first caught in an unfriendly manner when the user tries to save the field or record.

在Microsoft Access中以表格形式输入时间可能很困难。 没有像日期选择器这样的时间选择器,如果有的话,它对于许多用户来说太慢了。 更糟糕的是,验证仅是基本的,这意味着在用户尝试保存字段或记录时,错误通常首先以一种不友好的方式被捕获。

One way to help the user is to set an input mask, but that adds other problems for the user if he/she clicks inside the textbox or deletes one or more digits, so many users hate input masks. Also, validation is difficult. For example, if the first hour digit is zero or one, the second hour digit can be any digit, but if the first is two, the second can not be higher than four.

帮助用户的一种方法是设置输入掩码,但是如果用户在文本框内单击或删除一个或多个数字,则会给用户带来其他问题,因此许多用户讨厌输入掩码。 而且,验证是困难的。 例如,如果第一个小时数字为零或一个,则第二个小时数字可以为任何数字,但是如果第一个小时为2,则第二个数字不能大于4。

它应该如何工作 (How it should work)

To have a textbox that just works for entering time takes several steps:

要拥有一个用于输入时间的文本框,需要执行以下步骤:

  • Define an input mask that makes sense to the user

    定义对用户有意义的输入掩码
  • Handle positioning of the cursor no matter how the user enters the textbox

    无论用户如何输入文本框,都将处理光标的定位
  • Enable entering of digits no matter how the user navigates in the textbox

    无论用户如何在文本框中导航,都允许输入数字
  • Catch all invalid inputs

    捕获所有无效输入
  • Provide reasonable default values to correct invalid inputs

    提供合理的默认值以更正无效输入
  • Set a format for the textbox

    设置文本框的格式

That may seem like a lot, but the outcome is a highly optimised control demonstrating the true power of an input mask.

这看起来可能很多,但是结果是一个高度优化的控件,展示了输入掩码的真正功能。

正确处理需要很多时间 (It takes a lot to get it right)

The first and the last item are quite simple.

第一项和最后一项非常简单。

The input mask is, as the first digit is optional:

输入掩码是,因为第一位是可选的:

"90:00;1;0" “ 90:00; 1; 0”

and the format is:

格式为:

"hh:nn" “ hh:nn”

This makes the textbox contain as default:

这使文本框默认包含:

00:00 00:00

These can be set when the form loads:

可以在加载表单时设置这些:

Option Explicit

Dim DefaultTime     As Date

Private Sub Form_Load()
    
    Const InitialTime   As Date = #6:00:00 AM#
    
    Me!Logon.ShowDatePicker = False
    Me!Logon.InputMask = "90:00;1;0"
    Me!Logon.Format = "hh:nn"
    SetDefaultTime InitialTime

End Sub 

That's surprisingly little, but then comes the code behind for the control.

令人惊讶的是,这很少,但是随后是控件的代码。

First, set a default time. This may be dynamic (controlled by other code), but here it is static for simplicity:

首先,设置默认时间。 这可能是动态的(由其他代码控制),但为简单起见,这里是静态的:

Private Sub Logon_Enter()
  
    With Me!Logon
        If IsNull(.Value) Then
            .Value = DefaultTime
        End If
    End With

End Sub 

Second, when clicking in the control, avoid ever having to select the separator (colon):

其次,在控件中单击时,不必选择分隔符(冒号):

Private Sub Logon_Click()

    With Me!Logon
        If .SelStart = 2 Then
            ' Move the cursor off the separator (colon)
            ' to the first digit of minutes.
            .SelStart = 3
            .SelLength = 1
        End If
    End With

End Sub 

Now comes the fun part -- to try to be smart, helping the user by correcting invalid values while still allowing the normal key entries for navigating inside the control as well as away from the control:

现在出现了有趣的部分-尝试变得聪明,通过更正无效值来帮助用户,同时仍然允许常规键条目在控件内部以及远离控件的范围内导航:

Private Sub Logon_KeyPress(KeyAscii As Integer)
  
    Dim Text        As String
    Dim Char        As String
    Dim Length      As Integer
    Dim SelStart    As Integer
    
    With Me!Logon
        Select Case KeyAscii
            Case vbKeyBack, vbKeyTab, Asc(vbLf), vbKeyReturn, vbKeyEscape, vbKeyF16
                ' Allow navigation etc. with
                ' BackSpace, Tab, Ctrl+Enter, Enter, Escape, Ctrl+BackSpace
            Case Is > 0
                Text = .Text
                Length = Len(Text)
                SelStart = .SelStart
                
                If KeyAscii < vbKey0 Or KeyAscii > vbKey9 Then
                    ' Replace any invalid entry with a zero.
                    KeyAscii = vbKey0
                End If
                
                Char = Mid(Text, 1 + SelStart, 1)
                If SelStart < Length Then
                    If KeyAscii <= vbKey0 + 2 Then
                        ' Always accept 0, 1, 2.
                    Else
                        ' Check if the text will represent a valid time.
                        ' If not, restore the overwritten digit.
                        Mid(Text, 1 + SelStart, 1) = Chr(KeyAscii)
                        If Not IsDate(Text) Then
                            DoCmd.Beep
                            KeyAscii = Asc(Char)
                        End If
                    End If
                End If
        End Select
    End With

End Sub 

The in-line comments explain the function of the crucial parts of the code.

内嵌注释说明了代码关键部分的功能。

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 

Note, that it will always leave some time value in the control.

注意,它将始终在控件中保留一些时间值。

When done, you may set a new default value:

完成后,您可以设置一个新的默认值:

Private Sub Logon_AfterUpdate()

    With Me!Logon
        If IsNull(.Value) Then
            ' Rem this line out to allow the textbox to be cleared.
            .Value = #12:00:00 AM#
        Else
            SetDefaultTime DateAdd("n", 1, .Value)
        End If
    End With
    
End Sub


Private Sub SetDefaultTime(ThisTime As Date)

    DefaultTime = ThisTime
    Me!Logon.DefaultValue = Format(ThisTime, "\#hh:nn:00 AM/PM\#")

End Sub  

Note the conversion of the time value as a formatted string expression, as DefaultValue is a string.

注意时间值的转换为格式化的字符串表达式,因为DefaultValue是一个字符串。

看看这个 (Check it out)

To test it for yourself, download and run the demo application. It is a simple form created in Access 2013  that should work as is for all versions of Access from 2007 to 2016. However, the code origins from Access 2.0, so it should be adoptable with minor changes for any version of Access.

要自己测试,请下载并运行演示应用程序。 这是在Access 2013中创建的简单表单,该表单应适用于2007年至2016年的所有版本的Access。但是,代码起源于Access 2.0,因此可以对任何版本的Access进行少量更改即可采用。

You can also obtain the code from Github: VBA.TimeEntry

您也可以从Github获取代码: VBA.TimeEntry

I hope you found this article useful. You are encouraged to ask questions, report any bugs or make any other comments about it below.

希望本文对您有所帮助。 鼓励您在下面提出问题,报告任何错误或对此作出任何其他评论。

Note: If you need further "Support" about this topic, please consider using the Ask a Question feature of Experts Exchange. I monitor questions asked and would be pleased to provide any additional support required in questions asked in this manner, along with other EE experts.

注意 :如果您需要有关此主题的更多“支持”,请考虑使用Experts Exchange 的“提问”功能。 我会监督提出的问题,并很高兴与其他电子工程师一起为以这种方式提出的问题提供所需的任何其他支持。

Please do not forget to press the "Thumbs Up" button if you think this article was helpful and valuable for EE members.

如果您认为本文对EE成员有用且有价值,请不要忘记按下“竖起大拇指”按钮。

翻译自: https://www.experts-exchange.com/articles/23539/Entering-24-hour-time-with-input-mask-and-full-validation-in-Microsoft-Access.html

html 输入掩码密码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值