使用空格键选中复选框,在复选框上执行DCount(),焦点仍保持在复选框上?

huangapple go评论50阅读模式
英文:

Using spacebar to check a Check Box, perform a DCount(), with focus remaining on the check box?

问题

以下是已翻译的内容:

我有一个带有数据表子表单的表单,其中一个控件是复选框(Chk1)。

我希望它的行为是这样的:如果使用鼠标单击复选框,则立即在VBA中执行与同一复选框字段相关的DCount(...)。我已经实现了这部分,通过在复选框的AfterUpdate子过程中离开记录:

Public fromSpace As Boolean

Private Sub Chk1_AfterUpdate()
    If Not fromSpace Then 'mouse click, so it's necessary to fire Form_AfterUpdate by navigating away
        Form_Main.cboSelector.SetFocus
    Else 'Spacebar used with SendKeys so navigating away is not necessary
        fromSpace = False 'reset for next time
    End If
End Sub

然后,在数据表子窗体的AfterUpdate中执行DCount(...):

Private Sub Form_AfterUpdate()
    If DCount(...) = x Then 'This DCount looks at the yes/no (Check Box) field in question
        'Do some stuff
    End If
End Sub

但是,我还希望它的行为是这样的:如果使用空格键选中复选框,我希望发生相同的DCount(...),但同时保持焦点在子表单中的复选框上(这样用户可以继续使用键盘导航)。

我实际上已经实现了这一点,但是这是使用SendKeys("{F9}"):

Private Sub chk1_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeySpace Then
        fromSpace = True
        SendKeys ("{F9}") 'This WORKS (DCount(...) is performed, and focus remains on the Check Box). However, I understand using SendKeys is bad practice, so looking for a different solution
    End If
End Sub

我还找到了这个链接:https://www.reddit.com/r/vba/comments/tysrn7/click_a_button_on_the_ribbon/(它以编程方式执行Ribbon按钮单击),我进行了测试,它可以工作,但非常慢。每次使用空格键选中或取消选中复选框时都会有0.5 - 1秒的延迟。

肯定有一种方法可以做到这一点吧?

感谢阅读。

编辑:对于任何感兴趣的人,我已经详细说明了代码,以显示SendKeys方法如何正常工作。

英文:

I have a form with a datasheet subform, where one of the controls is a Check Box (Chk1).

I want it to behave such that: If the check box is checked using mouse click, a DCount(...) on the same Check Box field is performed in the VBA straight away. I have this part working, by navigating away from the record in the Check Box AfterUpdate Sub:

Public fromSpace As Boolean

Private Sub Chk1_AfterUpdate()
    If Not fromSpace Then 'mouse click, so it's necessary to fire Form_AfterUpdate by navigating away
        Form_Main.cboSelector.SetFocus
    Else 'Spacebar used with SendKeys so navigating away is not necessary
        fromSpace = False 'reset for next time
    End If
End Sub

Then the AfterUpdate of the datasheet subform has the DCount(...)

Private Sub Form_AfterUpdate()
	If DCount(...) = x Then 'This DCount looks at the yes/no (Check Box) field in question
		'Do some stuff
	End If
End Sub

However, I also want it to behave such that: If the check box is checked using the spacebar key, I would like the same DCount(...) to happen, BUT while keeping focus on the Check Box in the subform (this is so the user can continue to navigate using keyboard).

I actually have it working, but this is using SendKeys ("{F9}")

Private Sub chk1_KeyUp(KeyCode As Integer, Shift As Integer)
	If KeyCode = vbKeySpace Then
        fromSpace = True
		SendKeys ("{F9}") 'This WORKS (DCount(...) is performed, and focus remains on the Check Box). However, I understand using SendKeys is bad practice, so looking for a different solution

		'DoCmd.RunCommand (acCmdSaveRecord) (I have also tried this, which doesn't work)
		'DoCmd.RunCommand (acCmdSave) (This doesn't work either)
        
        'also tried doing the DCount(...) here, which doesn't fire until navigating away from the record
        'EDIT: Sorry, I think it does fire, but it doesn't use the new checkbox value in the DCount
	End If
End Sub

I also found this:
https://www.reddit.com/r/vba/comments/tysrn7/click_a_button_on_the_ribbon/ (which programmatically does a Ribbon button click), which I tested, and it works, BUT is very slow. There is a 0.5 - 1 second delay every time spacebar is used to check or un-check the Check Box.

Surely there's a way to do this?

Thanks for reading.

EDIT: For anyone who is interested, I have elaborated on the code a bit, to show how the SendKeys method is working properly.

答案1

得分: 0

不需要KeyUp事件。使用空格键来勾选/取消勾选复选框会触发与用鼠标单击它一样的复选框AfterUpdate事件。

以下内容对我有效,计算的数字总是立即更新。

所有代码都在数据表子窗体中,主窗体没有代码。

Option Compare Database
Option Explicit

Private Sub chkYesOrNo_AfterUpdate()

    ' 保存记录,触发Form_AfterUpdate() - 无论是用鼠标单击还是空格键都可以
    Me.Dirty = False

End Sub

Private Sub Form_AfterUpdate()

    Dim n As Long

    ' 计算n
    n = DCount("*", "tblSubform", "[YesOrNo] = -1")
    Debug.Print n

    ' 在主窗体上的一个非绑定文本框中显示数字
    Me.Parent!txtCount.Value = n

End Sub

Private Sub Form_Load()
    ' 在主窗体加载时显示数字
    Call Form_AfterUpdate
End Sub

在所有情况下,焦点都保留在复选框上。

英文:

You don't need the KeyUp event at all. Checking/unchecking the checkbox with the Space key triggers the checkbox AfterUpdate just the same as clicking it with the mouse.

The following works for me, the calculated number is always updated immediately.

All code is in the datasheet subform, the main form has no code.

Option Compare Database
Option Explicit

Private Sub chkYesOrNo_AfterUpdate()

    ' Save the record, triggering Form_AfterUpdate() - it doesn't matter if this is done with a mouse click or the space key
    Me.Dirty = False

End Sub

Private Sub Form_AfterUpdate()

    Dim n As Long
    
    ' Calculate n
    n = DCount("*", "tblSubform", "[YesOrNo] = -1")
    Debug.Print n
    
    ' Show number in an unbound text box on the main form
    Me.Parent!txtCount.Value = n
    
End Sub

Private Sub Form_Load()
    ' Show the number on main form load
    Call Form_AfterUpdate
End Sub

In all cases, the focus stays on the checkbox.

huangapple
  • 本文由 发表于 2023年5月22日 21:17:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定