【问题标题】:KeyDown Event Runs Only When I Arrow Into a Field, Not when I Tab into or Enter Key Into the FieldKeyDown 事件仅在我进入字段时运行,而不是在我进入字段或在字段中输入键时运行
【发布时间】:2020-07-13 21:05:00
【问题描述】:

我已经搜索过,但找不到任何有关此行为的帮助。 (我在 Access 2010 中工作,但数据库是 Access 2000 文件格式。)我正在使用数据表视图中的表单。如果按下向下箭头键,我编写了下面的代码以从上面的记录中复制库存位置。如果我使用向下箭头键从一个记录垂直向下移动到下一个记录,则代码可以正常工作,但如果我使用 Tab 或 Enter 键从一个字段移动到下一个字段,则代码不会。

Private Sub InventoryLocation_KeyDown(KeyCode As Integer, Shift As Integer)
    ' Variables are defined as Public at head of module.
    If KeyCode = vbKeyDown Then
        If Me.CurrentRecord = intPreviousRecordNumber + 1 Then
            If IsNull(Me.InventoryLocation.Value) Then
                Me.InventoryLocation.Value = varPreviousInventoryLocation
                DoCmd.CancelEvent
            End If
        End If
    End If
End Sub

正在从此处的先前记录中填充变量:

Private Sub InventoryLocation_LostFocus()
    ' Variables are defined as Public at head of module.
    varPreviousInventoryLocation = Me.InventoryLocation.Value
    intPreviousRecordNumber = Me.CurrentRecord
End Sub

这是我的第一篇文章,但我可以在 StackOverflow.com 上找到我的大部分答案。任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 你能从那个控件的 On Got Focus 事件中做你想做的事吗?
  • 我选择使用 On Key Down,因为我需要让我的用户决定何时复制库存位置。
  • 不过,我刚刚解决了这个问题。我已经为此苦苦挣扎了三天,当然,一旦我提出问题,我就弄清楚了!我的错误显然是我认为我的变量 intPreviousRecordNumber 可以重复用于记录中的多个字段。 (我还有一个名为 OverstockLocation 的字段,其 LostFocus 事件正在设置 intPreviousRecordNumber 的值。我认为这很好,因为同一记录中的两个字段的值相同。)
  • 显然 Access 反对 InventoryLocation 和 OverstockLocation 尝试使用相同的变量。我创建了一个名为 intPreviousRecordNumberOverstock 的新变量,供 OverstockLocation 代码使用,现在两者都按预期工作。我仍然不知道为什么。我会认为一个行号变量可以被一个或多个引用该行号的字段使用。顺便感谢您的快速回复!

标签: ms-access vba


【解决方案1】:

我的错误显然是我认为我的变量 intPreviousRecordNumber 可以用于记录中的多个字段。 (我还有一个名为 OverstockLocation 的字段,其 LostFocus 事件正在设置 intPreviousRecordNumber 的值。我认为这很好,因为同一记录中的两个字段的值相同。) 显然 Access 反对 InventoryLocation 和 OverstockLocation 尝试使用相同的变量。我创建了一个名为 intPreviousRecordNumberOverstock 的新变量,供 OverstockLocation 代码使用,现在两者都按预期工作。

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2013-07-30
    • 2021-08-05
    • 2011-11-02
    • 2015-08-22
    相关资源
    最近更新 更多