数据录入经常用到组合框。制作了个函数,能够在组合框中输入非绑定列数据后返回绑定列数据。
要求:1、将组合框的“限于列表”属性设为“否”;
2、组合框的“列数”属性设为大于1的数;
3、组合框的“绑定列”属性设为1。
Public Function funGetCboValue(ByVal cboComboBox As ComboBox) As Integer
If Nz(cboComboBox.Text, "") = "" or cboComboBox.ListCount = 0 or cboComboBox.ColumnCount = 0 Then Exit Function
Dim intH As Integer
Dim intL As Integer
For intH = 0 To cboComboBox.ListCount -1 '循环记录源的行
For intL = 0 To cboComboBox.ColumnCount -1 '循环记录源的列
If cboComboBox = cboComboBox.Column(intL, intH) Then '在行来源中存在输入的值
cboComboBox = cboComboBox.Column(0, intH) '组合框的值转为绑定列的值
Exit Function
End If
Next intL
Next intH
MsgBox "输入的值不在列表值范围内!请重新输入。", vbExclamation, "输入错误"
cboComboBox.SelStart = 0
cboComboBox.SelLength = Len(cboComboBox.Text)
funGetCboValue = True
End Function
调用:
Private Sub 性别_Exit(Cancel As Integer)
Cancel = funGetCboValue(性别)
End Sub
动画演示:
附 件:
点击下载此附件