This tutorial explains how to use Combo Box in Access Form and save selected value for use in another Form or Report.
Access Form Combo Box
Combo Box is also known as Dropdown Box, which allows users to select data from a list by clicking on the down arrow button. The advantage of Combo Box is that you can have control over what users will input, this will prevent unexpected input and typo.
Because Combo Box receives user input, Combo Box should be used in Access Form. If you try to open a Access Report based on search criteria, use Combo Box in Form and pass the Combo Box selection to Report Control Source, do not directly place a Combo Box in Report, because Report is used to display result.
Create Access Form Combo Box
To insert a Combo Box in a Form, navigate to Design tab and select Combo Box and drag down to the Form.
Combo Box Property – Row Source / Row Source Type
Navigate to Combo Box Properties > Data > Row Source, this is where you set the Combo Box values.
Select one of the three Row Source Type depending on where your source come from.
1) Row Source Type = Table/Query
You can select a Table Name, Query Name, or build a new Query in Query Builder by clicking on the … button on the right. The Table/Query can be different from the Control Source of the Form.
– If you directly select a Table name/Query Name that contains multiple fields as Row Source, only the first field is used in Combo Box list. To display more than one fields in Combo Box, navigate to the Combo Box Property > Format > Column Count
Below is an example of showing Column Count is set to 2
For multiple columns, you may also need to set the Column Width Property (Combo Box Property > Format > Column Width), your input should be separated by semi column for each column width.
The below example shows Column Width = 1;1 (or 1cm;1cm)
Set Column Heads Property to Yes to display field headers (Combo Box Property > Format > Column Headers)
-If the list you select contains duplicate values, write a SQL statement with DISTINCT keyword to remove duplicates .
For example, SELECT DISTINCT [EmplID] FROM [JOB_Tbl]
– If you want to sort Combox Data, use Query builder to sort or directly type SQL command
For example, SELECT DISTINCT [EmplID] FROM [JOB_Tbl] SORT BY [EmplID]
2) Row Source Type = Value List
If you select Value List in Row Source Type, you will see a prompt in Row Source to define the list of Combo Box items, then it will convert this table to a string expression.
3) Row Source Type = Field List
Field List means the Field Name for the table or query. If you choose Field List in Row Source Type, and then select a Table name, you can choose the field name in Combo Box.
At this point, the basic setup of Combo Box is complete.
Form Property – Allow Edit
Allow Edit Property allows you edit data in Form. By default, the value is set to Yes, you must set to Yes in order for Combo Box to work.
Navigate to Form Properties > Data > Allow Edit, make sure the parameter is set to Yes
Event for Combo Box
Now that a user has selected a value in Combo Box, so what’s next?
You can choose one of the below Events to trigger saving action of the selected Combo Box value
– Create a submit Button in Form to trigger a Click Event
– Use AfterUpdate Event for the Combo Box
– Use Change Event for Combo Box (not recommended)
AfterUpdate Event is triggered when you select a value from Combo Box.
Change Event is triggered whenever the value is different. If you directly type something in Combo Box, the Event is trigger for every character you type in.
VBA to save Combo Box value
To save Combo Box value as variable for later use, define a variable as Global variable in Module.
Press ALT+F11 > insert a Module > copy and paste the below code. This will declare a global variable emplID_global.
Public emplID_global
In the VBE environment, double click on the Form containing the Combo Box, save the Combo Box value as emplID_global.
If Not IsNull(Me.Combo_emplID) Then
emplID_global = Me.Combo_emplID
End If
Now you have saved the Combo Box value, and you can use the variable emplID_global in any Form or Module.
In the Report or Form that you want to use that variable, create a SQL string in VBA that uses this variable, then use the SQL as Record Source of the Report or Form.
You may find a detailed example in my other article below.
Return Access Form input to Access Report or Form or Query
Outbound References
Access Form Combo Box and save selected value
沒有留言:
張貼留言