以下示例演示在加载窗体时如何设置组合框的“RowSource”属性。 当显示该窗体时,存储在“tblDepartment”组合框的“Department”字段中的项目将显示在“cboDept”组合框中。
Private Sub Form_Load()
Me.Caption = "Today is " & Format$(Date, "dddd mmm-d-yyyy")
Me.RecordSource = "tblDepartments"
DoCmd.Maximize
txtDept.ControlSource = "Department"
cmdClose.Caption = "&Close"
cboDept.RowSourceType = "Table/Query"
cboDept.RowSource = "Select Department FROM tblDepartments"
End Sub
下面的示例演示如何创建在显示另一列时绑定到一列的组合框。 将“ColumnCount”属性设置为 2 可指定“cboDept”组合框将显示由“RowSource”属性指定的数据源的前两列。 将“BoundColumn”属性设置为 1 可指定在检查组合框的值时将会返回存储在第一列中的值。
ColumnWidths 属性指定两列的宽度。 通过将第一列的宽度设置为 0in.,可以在组合框中不显示第一列。
Private Sub cboDept_Enter()
With cboDept
.RowSource = "Select * FROM tblDepartments orDER BY Department"
.ColumnCount = 2
.BoundColumn = 1
.ColumnWidths = "0in.;1in."
End With
End Sub
以下示例演示如何将项目添加到绑定的组合框。
Private Sub cboMainCategory_NotInList(NewData As String, Response As Integer)
On Error GoTo Error_Handler
Dim intAnswer As Integer
intAnswer = MsgBox("""" & NewData & """ is not an approved category. " & vbcrlf _
& "Do you want to add it now?", vbYesNo + vbQuestion, "Invalid Category")
Select Case intAnswer
Case vbYes
DoCmd.SetWarnings False
DoCmd.RunSQL "Insert INTO tlkpCategoryNotInList (Category) " & _
"Select """ & NewData & """;"
DoCmd.SetWarnings True
Response = acDataErrAdded
Case vbNo
MsgBox "Please select an item from the list.", _
vbExclamation + vbOKOnly, "Invalid Entry"
Response = acDataErrContinue
End Select
Exit_Procedure:
DoCmd.SetWarnings True
Exit Sub
Error_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Procedure
Resume
End Sub