vba - 简化条件 VBA 代码 Microsoft Access
问题描述
我是 MS Access 和 VBA 代码的新手,但我一直在处理我的需求。下面发布的代码是我让 Click Event 工作的唯一方法。为了从多个表中打印当前记录(邮件合并),需要填写一些字段。因此,在实际打印输出代码之前,我插入了以下代码。有更好的方法吗?只是感觉不像我做的那样。
If IsNull(Screen.ActiveForm![Nome]) Then
MsgBox "Preencher o Nome do Cliente."
Screen.ActiveForm![Nome].SetFocus
Else
If IsNull(Screen.ActiveForm![Gênero]) Then
MsgBox "Preencher o Gênero do Cliente."
Screen.ActiveForm![Gênero].SetFocus
Else
If IsNull(Screen.ActiveForm![Estado Civíl]) Then
MsgBox "Preencher o Estado Civíl do Cliente."
Screen.ActiveForm![cboecivil].SetFocus
Else
If IsNull(Screen.ActiveForm![Profissão]) Then
MsgBox "Preencher a Profissão do Cliente."
Screen.ActiveForm![Profissão].SetFocus
Else
If IsNull(Screen.ActiveForm![CEP]) Then
MsgBox "Preencher o CEP do Cliente."
Screen.ActiveForm![CEP].SetFocus
Else
If IsNull(Screen.ActiveForm![Endereço]) Then
MsgBox "Preencher o nome da Rua do Cliente."
Screen.ActiveForm![Endereço].SetFocus
Else
If IsNull(Screen.ActiveForm![Número]) Then
MsgBox "Preencher o Número da Rua do Cliente."
Screen.ActiveForm![Número].SetFocus
Else
If IsNull(Screen.ActiveForm![Cidade]) Then
MsgBox "Preencher a Cidade do Cliente."
Screen.ActiveForm![Cidade].SetFocus
Else
If IsNull(Screen.ActiveForm![UF]) Then
MsgBox "Preencher o Estado do Cliente."
Screen.ActiveForm![UF].SetFocus
Else
If IsNull(Screen.ActiveForm![Bairro]) Then
MsgBox "Preencher o Bairro do Cliente."
Screen.ActiveForm![Bairro].SetFocus
Else
If IsNull(Screen.ActiveForm![Complemento]) Then
MsgBox "Preencher o Complemento do Endereço do Cliente."
Screen.ActiveForm![Complemento].SetFocus
Else
If IsNull(Forms("Painel de Controle").sftblCPF.Form.CPF) Then
MsgBox "Preencher o CPF do Cliente."
Forms("Painel de Controle").sftblCPF.Form.CPF.SetFocus
Else
If IsNull(Forms("Painel de Controle").sftblRG.Form.Número) Then
MsgBox "Preencher o Número do RG do Cliente."
Forms("Painel de Controle").sftblRG.Form.Número.SetFocus
Else
If IsNull(Forms("Painel de Controle").sftblRG.Form.Série) Then
MsgBox "Preencher a Série do RG do Cliente."
Forms("Painel de Controle").sftblRG.Form.Série.SetFocus
Else
If IsNull(Forms("Painel de Controle").sftblRG.Form.[Orgão Emissor]) Then
MsgBox "Preencher o Orgão Emissor do RG do Cliente."
Forms("Painel de Controle").sftblRG.Form.[Orgão Emissor].SetFocus
Else
If Forms("Painel de Controle").sftblCPF.Form.[Principal?] = False Then
MsgBox "Marcar o CPF Principal do Cliente."
Forms("Painel de Controle").sftblCPF.Form.[Principal?].SetFocus
Else
If Forms("Painel de Controle").sftblRG.Form.[Principal?] = False Then
MsgBox "Marcar o RG Principal do Cliente."
Forms("Painel de Controle").sftblRG.Form.[Principal?].SetFocus
Else
'MailMerge code inserted Here.
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
解决方案
将所有字段名称放入一个数组中
Dim fieldNames As Variant
Private Sub Form_Load()
fieldNames = Array("Nome", "Gênero", "Estado Civíl", "Profissão", ...)
End Sub
然后使用循环进行检查
Dim fieldName As String
Dim i As Long
For i = LBound(fieldNames) To UBound(fieldNames)
fieldName = fieldNames(i)
If IsNull(Screen.ActiveForm(fieldName).Value) Then
MsgBox "Preencher o " & fieldName & " do Cliente."
Screen.ActiveForm(fieldName).SetFocus
Exit Sub
End If
Next i
如果您需要单独组合的消息,您可以使用带有消息的第二个数组:
Dim fieldNames As Variant
Dim messages As Variant
Private Sub Form_Load()
fieldNames = Array("Nome", "Gênero", "Estado Civíl", "Profissão", ...)
messages = Array("Preencher o Nome do Cliente.", "Preencher o Gênero ...", ...)
End Sub
然后再次使用循环进行检查
Dim fieldName As String
Dim i As Long
For i = LBound(fieldNames) To UBound(fieldNames)
fieldName = fieldNames(i)
If IsNull(Screen.ActiveForm(fieldName).Value) Then
MsgBox messages(i)
Screen.ActiveForm(fieldName).SetFocus
Exit Sub
End If
Next i
顺便说一句,您可以使用 anElseIf
而不是Else
后跟 an If
。这将链接条件而不是嵌套它们
If IsNull(Screen.ActiveForm![Nome]) Then
MsgBox "Preencher o Nome do Cliente."
Screen.ActiveForm![Nome].SetFocus
ElseIf IsNull(Screen.ActiveForm![Gênero]) Then
MsgBox "Preencher o Gênero do Cliente."
Screen.ActiveForm![Gênero].SetFocus
ElseIf IsNull(Screen.ActiveForm![Estado Civíl]) Then
MsgBox "Preencher o Estado Civíl do Cliente."
Screen.ActiveForm![cboecivil].SetFocus
ElseIf IsNull(Screen.ActiveForm![Profissão]) Then
MsgBox "Preencher a Profissão do Cliente."
Screen.ActiveForm![Profissão].SetFocus
...
End If
推荐阅读
- javascript - Mongoos -> Model.update() -> SyntaxError: 位置 80 处 JSON 中的意外字符串
- python - setuptools 生成的项目命令工作正常,但 pip install tar 包给出错误模块未找到
- javascript - 如何使用 vanilla javascript 在节点服务器上执行功能
- excel - 如何使用用户窗体从 Excel 中检索数据
- postgresql - 如何在列中创建带有切片的表
- r - 跨多个列运行函数
- python - 读取公共 json 文件时出现 Python FileNotFoundError
- javascript - 如何使用触发函数来控制显示或隐藏div
- javascript - React 组件未显示在浏览器上,屏幕为空
- r - 正则表达式提取数字和尾随字母或空格