验证vba 7.1中的组合框

时间:2015-11-30 23:23:36

标签: vba validation combobox ms-access-2013

我正在尝试验证访问表单上的一系列组合框,以便用户在处理表单时不能将它们留空。我尝试过使用各种示例,但我得到了相同的错误 - “预期结束语”

可能非常容易。我一直在玩的代码示例是:

Private Sub Combobox1_Validating(sender As Object, e As  System.ComponentModel.CancelEventArgs) Handles Combobox1.Validating
    If Combobox1.Items.Contains(Combobox1.Text) = False Then
        e.Cancel = True
    End If
End Sub

Private Sub Combobox1_Leave(sender As Object, e As System.EventArgs) Handles Combobox1.Leave
    If Combobox1.Items.Contains(Combobox1.Text) = False Then
        Combobox1.Select()
        MessageBox.Show("select item from combobox")
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

更改MatchRequired属性:

enter image description here

无论您使用哪个OK按钮,都可以添加以下代码:

If ComboBox1.Value = "" Then
    MsgBox ("Nice try, now fill the combo box in right this time!")
    Exit Sub
end if

...并丢弃所有VB.Net代码!

希望这有帮助!

答案 1 :(得分:0)

现在使用以下代码解决问题 - 感谢您的所有贡献

Private Sub Process_Button_Click()

If Patient_ID_Combo.ListIndex = -1 Then
    MsgBox ("Nice try, now fill the combo box in right this time!")
End If
If Doctor_ID_combo.ListIndex = -1 Then
    MsgBox ("Please choose a Doctor")
End If
If Product_combo.ListIndex = -1 Then
    MsgBox ("Please choose a product")   
Else
    MsgBox ("You are about to update your records press ok to continue")
    DoCmd.OpenQuery "Update Stock", , acEdit
    DoCmd.OpenQuery "Update Prescriptions", , acAdd
    Exit Sub
End If
End Sub
相关问题