【问题标题】:Allow user to separate dates with period in MS Access允许用户在 MS Access 中用句点分隔日期
【发布时间】:2011-06-22 17:21:36
【问题描述】:

我正在使用一个 Access 数据库,其中有一个包含多个日期输入字段的表单。我有一个新用户习惯于使用句点作为日期的分隔符,因此他们输入的是“6.22.11”而不是“6/22/11”或“6-22-11”。我想继续允许这种类型的条目,但 Access 将“6.22.11”转换为时间而不是日期。我尝试在没有帮助的情况下将文本框上的格式设置为“短日期”。我也尝试将代码添加到“失去焦点”事件中,但由于 Access 已经完成了转换,所以为时已晚。 “更新前”事件在转换之前触发,但不允许我更改文本框中的文本。关于如何允许所有三种形式的日期输入有什么想法吗?

【问题讨论】:

  • 我已经开始查看文本框的 KeyPress 事件,它看起来应该能够替换 '.'在他们键入时使用“/”,但我也无法让它工作。如果我设置断点,代码似乎可以工作,但在文本框中输入的文本不会改变。 Private Sub Lot_Date_KeyPress(KeyAscii As Integer) If Chr(KeyAscii) = "." Then KeyAscii = Asc("/") End If End Sub
  • 告诉用户克服它并以标准方式输入日期。
  • David 是对的……您甚至不应该考虑在此类非问题上浪费一分钟的时间。输入日期有一种标准方法,例如使用键盘或鼠标的标准方法,用户必须遵守这些规则。

标签: ms-access date data-entry


【解决方案1】:

你上面的例子

Private Sub Texto0_KeyPress(KeyAscii As Integer)
    If Chr(KeyAscii) = "." Then
        KeyAscii = Asc("/")
    End If
End Sub

为我工作。

另一个近似值是使用 BeforeUpdate 和 AfterUpdate 事件。 在更新前您不能修改控件的内容,但您可以在更新后事件中设置一个标志(在模块/表单级别定义的变量)并更改内容:它将再次触发更新前,但在这种情况下,因为是标记你应该忽略它并取消标记。

【讨论】:

  • 由于某种原因,我仍然无法让 KeyPress 事件正常工作。我能够利用 BeforeUpdate 和 AfterUpdate 事件来使用你的想法,它就像一个魅力。谢谢!
【解决方案2】:

【讨论】:

  • 我希望避免输入掩码,因为根据我的经验,它们在输入数据时很难使用。
【解决方案3】:

我为一个习惯于在输入掩码中输入 6 位和 8 位日期的用户编写了以下函数,只需键入一串不带分隔符的数字。您应该可以根据自己的目的对其进行修改:

'---------------------------------------------------------------------------
' Purpose   : Enables entry of 8-digit dates with no delimiters: 12312008
' Usage     : Set OnChange: =DateCtlChange([Form].[ActiveControl])
'  8/ 6/09  : Allow entry of 6-digit dates with no delimiters
'               (year 2019 and 2020 must still be entered as 8-digit dates)
'---------------------------------------------------------------------------
Function DateCtlChange(DateCtl As TextBox)
Dim s As String, NewS As String

    On Error GoTo Err_DateCtlChange

    s = DateCtl.Text
    Select Case Len(s)
    Case 6
        If s Like "######" Then
            If Right(s, 2) <> "19" And Right(s, 2) <> "20" Then
                NewS = Left(s, 2) & "/" & Mid(s, 3, 2) & "/" & Mid(s, 5, 2)
            End If
        End If
    Case 8
        If s Like "########" Then
            NewS = Left(s, 2) & "/" & Mid(s, 3, 2) & "/" & Mid(s, 5, 4)
        End If
    End Select
    If IsDate(NewS) Then
        DateCtl.Text = NewS
        DateCtl.SelStart = Len(DateCtl.Text)
    End If


Exit_DateCtlChange:
    Exit Function
Err_DateCtlChange:
    Select Case Err.Number
    'Error 2101 is raised when we try to set the text to a date 
    '    that fails the date control's validation
    Case 2101    'The setting you entered isn't valid for this property.
        'Log error but don't show user
    Case Else
        'Add your custom error logging here
    End Select
    Resume Exit_DateCtlChange
End Function

【讨论】:

    【解决方案4】:

    Access 使用系统日期和时间格式来确定如何转换值。另一个选项 - 会影响此用户计算机上的每个程序 - 是:http://office.microsoft.com/en-us/access-help/change-the-default-date-time-number-or-measurement-format-HA010351415.aspx?CTT=1#BM2

    【讨论】:

      【解决方案5】:

      或者,您可以在 Access 中的日期中使用空格而不是斜杠。因此,用户可以在空格键上使用左手,在数字键盘上使用右手。我觉得这比斜线或连字符更容易使用。

      【讨论】:

        猜你喜欢
        • 2021-06-02
        • 2010-11-29
        • 1970-01-01
        • 2021-02-19
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 2015-06-24
        相关资源
        最近更新 更多