ComboBox Object (Access)
This object corresponds to a combo box control. The combo box control combines the features of a text box and a list box. Use a combo box when you want the option of either typing a value or selecting a value from a predefined list.
Remarks
Control: |
Tool: |
In Form view, Microsoft Access doesn't display the list until you click the combo box's arrow.
If you have Control Wizards on before you select the combo box tool, you can create a combo box with a wizard. To turn Control Wizards on or off, click the Control Wizards tool in the toolbox.
The setting of the LimitToList property determines whether you can enter values that aren't in the list.
The list can be single- or multiple-column, and the columns can appear with or without headings.
Link provided by: Luke Chung, FMS, Inc. | About the Contributors
Links provided by: The UtterAccess community | About the Contributors
Example
The following example shows how to use multiple ComboBox controls to supply criteria for a query.
Sample code provided by: The UtterAccess community | About the Contributors
UtterAccess members can download a database that contains this example from here.
Private Sub cmdSearch_Click()
Dim db As Database
Dim qd As QueryDef
Dim vWhere As Variant
Set db = CurrentDb()
On Error Resume Next
db.QueryDefs.Delete "Query1"
On Error GoTo 0
vWhere = Null
vWhere = vWhere & " AND [PymtTypeID]=" + Me.cboPaymentTypes
vWhere = vWhere & " AND [RefundTypeID]=" + Me.cboRefundType
vWhere = vWhere & " AND [RefundCDMID]=" + Me.cboRefundCDM
vWhere = vWhere & " AND [RefundOptionID]=" + Me.cboRefundOption
vWhere = vWhere & " AND [RefundCodeID]=" + Me.cboRefundCode
If Nz(vWhere, "") = "" Then
MsgBox "There are no search criteria selected." & vbCrLf & vbCrLf & _
"Search Cancelled.", vbInformation, "Search Canceled."
Else
Set qd = db.CreateQueryDef("Query1", "SELECT * FROM tblRefundData WHERE " & _
Mid(vWhere, 6))
db.Close
Set db = Nothing
DoCmd.OpenQuery "Query1", acViewNormal, acReadOnly
End If
End Sub
The following example shows how to set the RowSource property of a combo box when a form is loaded. When the form is displayed, the items stored in the Departments field of the tblDepartment combo box are displayed in the cboDept combo box.
Sample code provided by: Bill Jelen, MrExcel.com | About the Contributors
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
The following example show how to create a combo box that is bound to one column while displaying another. Setting the ColumnCount property to 2 specifies that the cboDept combo box will display the first two columns of the data source specified by the RowSource property. Setting the BoundColumn property to 1 specifies that the value stored in the first column will be returned when you inspect the value of the combo box.
The ColumnWidths property specifies the width of the two columns. By setting the width of the first column to 0in., the first column is not displayed in the combo box.
Sample code provided by: Bill Jelen, MrExcel.com | About the Contributors
Private Sub cboDept_Enter()
With cboDept
.RowSource = "SELECT * FROM tblDepartments ORDER BY Department"
.ColumnCount = 2
.BoundColumn = 1
.ColumnWidths = "0in.;1in."
End With
End Sub
The following example shows how to add an item to a bound combo box.
Sample code provided by: The Microsoft Access 2010 Programmer’s Reference | About the Contributors
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 & ", " & Error Description
Resume Exit_Procedure
Resume
End Sub
About the Contributors
Luke Chung은 유명한 사용자 지정 데이터베이스 솔루션 및 개발자 도구 공급업체인 FMS, Inc.(영문일 수 있음)의 설립자이자 사장입니다.
UtterAccess(영문일 수 있음)는 Microsoft Access 위키 및 도움말 포럼입니다. 가입하려면 여기를 클릭하십시오(영문일 수 있음).
Holy Macro! Books에서는 Microsoft Office 사용자를 위한 재미있는 서적을 다수 출판하고 있습니다. 전체 카탈로그는 MrExcel.com(영문일 수 있음)에서 확인할 수 있습니다.
Wrox Press(영문일 수 있음)는 프로그래머가 프로그래머를 위해 집필한 서적을 전문으로 출판하는 업체로, 이러한 서적에서는 실제 프로그래밍 관련 문제에 대한 신뢰할 수 있는 해결 방법을 제공합니다.