MS Access VBA,仅在所有必需的文本框包含有效数据后启用按钮的有效方法

MS Access VBA, efficient way to enable a button only after all required textboxes contains valid data

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

我有这样的布局形式:

为了让记录创建过程万无一失,我只想启用 "Save and Clear fields" 按钮毕竟 'Comment' textbox/combobox 包含一些有效数据。

text/combo 框称为 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 语句中。此子在每个 text/combo 框的 After_Update() 事件中调用。像这样:

Private Sub cmbChemistryType_AfterUpdate()
    Call EnableSaveBtnCheck
End Sub

最后我的问题是:是否有更有效的方法来设置条件"all text/combo box need to have something valid in them"?是否有更详细的方法来检查是否满足条件(类似于表单本身的事件)?

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

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