访问:VBA 代码按钮可见性不起作用

ACCESS : VBA Code Button Visibility not working

我在隐藏我的按钮时遇到问题并且无法正常工作主要是当按钮本身被单击时,它应该被隐藏。 我的一个按钮下面有一个代码,但大多数按钮可见性代码都不起作用。我尝试添加:

DoEvents

但还是不行。我不知道是什么问题。某些启用属性有效,但有些无效。

Private Sub cmdSave_Click()

If MsgBox("Are you sure to save record?", vbYesNo, "Save Record") = vbYes Then

DoEvents

    imgPic.Enabled = False
    txtLRN.Enabled = False
    txtLN.Enabled = False
    txtFN.Enabled = False
    txtMN.Enabled = False
    txtS.Enabled = False
    txtA.Enabled = False
    txtBD.Enabled = False
    txtBP.Enabled = False
    txtMT.Enabled = False
    txtIP.Enabled = False
    txtRG.Enabled = False
    txtAH.Enabled = False
    txtAB.Enabled = False
    txtAM.Enabled = False
    txtAP.Enabled = False
    txtF.Enabled = False
    txtM.Enabled = False
    txtGN.Enabled = False
    txtGR.Enabled = False
    txtCN.Enabled = False
    txtR.Enabled = False

DoEvents
    cmdSearch.Visible = True
    cmdFirst.Visible = True
    cmdPrev.Visible = True
    cmdNext.Visible = True
    cmdNext.Visible = True
    cmdLast.Visible = True
    cmdNew.Visible = True
    cmdDelete.Visible = True
    cmdEdit.Visible = True


DoEvents
    cmdSave.Visible = False
    cmdCancel.Visible = False

DoEvents

End if

End Sub

在类似的情况下,我发现您需要指定表单参考 ("Me.")。

所以尝试(例如)

    Me.cmdCancel.Visible = True

也就是在控件名称前面指定"Me."。

您的程序的这个简化版本将触发错误 #2165,"You can't hide a control that has the focus."

Private Sub cmdSave_Click()
    Me.cmdSave.Visible = False
End Sub

cmdSave_Click 期间,cmdSave 具有焦点,因此您必须先将焦点移动到另一个未隐藏的位置才能隐藏它控制。此版本将避免该错误:

Private Sub cmdSave_Click()
    Me.cmdSearch.Visible = True
    Me.cmdSearch.SetFocus
    Me.cmdSave.Visible = False
End Sub