首页 > 解决方案 > MS Access VBA,只有在所有必需的文本框都包含有效数据后才启用按钮的有效方法

问题描述

我的代码正在运行,但我只想知道是否有更有效的方法来实现相同的效果。

我有这种布局形式:

表单布局

为了使记录创建过程变得简单,我希望只启用“保存和清除字段”按钮,但“评论”文本框/组合框包含一些有效数据。

文本/组合框称为 txtBatteryID、cmbModelNumber、cmbChemistryType、txtSpecVoltage、txtSpecCapacity。

我的代码如下

Private Sub EnableSaveBtnCheck()
'this checks if the required fields contains valid data, if so, enables the save button.
    If Me.btnSaveAndCLear.Enabled = False Then
        If IsNull(txtBatteryID) = False And IsNull(cmbModelNumber) = False And IsNull(cmbChemistryType) = False And IsNull(txtSpecVoltage) = False And IsNull(txtSpecCapacity) = False Then
            Me.btnSaveAndCLear.Enabled = True
        End If
    End If
End Sub

如您所见,我采用了最直接的方式,使用 AND 将所有必备条件组合到 IF 语句中。这个子在每个文本/组合框的 After_Update() 事件中调用。像这样:

Private Sub cmbChemistryType_AfterUpdate()
    Call EnableSaveBtnCheck
End Sub

最后,我的问题是:是否有更有效的方法来设置条件“所有文本/组合框都需要在其中包含有效的内容”?是否有更精细的方法来检查条件是否满足(类似于表单本身的事件)?

标签: ms-accessvbams-access-2016

解决方案


添加这 5 个字段的值。如果其中任何一个为 Null,则总和将为 Null。所以你只需要调用IsNull()一次。

If IsNull(txtBatteryID + cmbModelNumber + cmbChemistryType + txtSpecVoltage + txtSpecCapacity) = False Then

推荐阅读