ms-access - 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
最后,我的问题是:是否有更有效的方法来设置条件“所有文本/组合框都需要在其中包含有效的内容”?是否有更精细的方法来检查条件是否满足(类似于表单本身的事件)?
解决方案
添加这 5 个字段的值。如果其中任何一个为 Null,则总和将为 Null。所以你只需要调用IsNull()
一次。
If IsNull(txtBatteryID + cmbModelNumber + cmbChemistryType + txtSpecVoltage + txtSpecCapacity) = False Then
推荐阅读
- elasticsearch-rails - rails elasticsearch - 条件索引
- django - django-filter 上的 ModelChoiceFilter
- matlab - 从图像中提取 CMY 颜色分量
- javascript - 我想显示一个页面的数据并使用 chrome 扩展显示到网站的另一个页面中?
- powerbi - 显示正确的目标线
- wget - wget 递归下载所有子目录中的特定文件类型
- scala - Spark:以指定的日期格式读取 csv
- flutter - Flutter 应用程序上的 flutter_dotenv 出现空安全错误
- php - 购买多件商品,但只扣入钱包一次
- python - 使用引用自身的类型进行键入