带有相应文本字段的组合框的验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有相应文本字段的组合框的验证相关的知识,希望对你有一定的参考价值。

  1. Private Sub cboColor_BeforeUpdate(Cancel As Integer)
  2.  
  3. ' Validation for combo box with a corresponding text field.
  4. ' When users select "Other" in cboColor, they are encouraged to specify in txtOther what they mean.
  5. ' If they enter something in txtOther and then decide to change or delete their answer in cboColor, this routine
  6. ' tells them the data in txtOther will be deleted. This prevents orphaned data being stored in txtOther
  7. ' when cboColor is blank or <> "Other".
  8.  
  9. If Not IsNull(Me.txtOther) Then ' If txtOther has data
  10. If Not Me.cboColor.Value = "Other" Or IsNull(Me.cboColor) Then ' and the user selects something different than "Other", ask the user to confirm the change
  11. If MsgBox("Changing this answer from 'Other' will delete the information in the adjacent text field. " & _
  12. Chr(13) & Chr(13) & "Continue?", vbQuestion + vbYesNo + vbDefaultButton2, "Change Answer?") = vbYes Then
  13. ' If the user clicks 'Yes'
  14. Me.txtOther = Null ' delete the data in txtOther
  15. Else ' Otherwise, if the user doesn't click 'Yes'
  16. Cancel = True ' don't go through with the change (i.e., cancel the update),
  17. Me.cboColor.Undo ' restore the value of cboOther to the original answer (i.e., 'Other')
  18. Exit Sub ' and leave this routine
  19. End If
  20. End If
  21. End If
  22.  
  23. 'Check the final value in cboColor to determine whether to enable or disable txtOther.
  24. 'If cboColor = 'Other', set txtOther's enabled property to True (i.e., -1).
  25. 'If cboColor <> 'Other', set txtOther's enabled property to False (i.e., 0).
  26.  
  27. Me.txtOther.Enabled = IIf(Me.cboColor = "Other", -1, 0)
  28.  
  29. End Sub

以上是关于带有相应文本字段的组合框的验证的主要内容,如果未能解决你的问题,请参考以下文章

Excel VBA检查各种组合框中是不是存在值,然后添加相应的文本框值

将组合框的值作为字段传递

带有按钮、组合框和文本框的 C# WinForms (.NET Framework) DataGridView:使用按钮添加新行以添加组合框项时出错

加入组合框的 ID

使用宏过滤带有组合框的表单 - MS Access

将可检查组合框的文本显示到 QTableWidget