I have a form in my Access database called:
- formAcademyRegister
Inside this form I have a combo box called:
- EmployeeCode
This combo box retrieves its data from a table called:
- Employees
The primary key on this field is EmployeeID
To make it easier for the user I created a form called formsEmployeeSearch. In this form use some text fields to make it easier to search for the employee using a first name or last name, returing the values to a list box on this form. This list box has three fields:
- EmployeeID
- FirstName
- LastName
All these fields are also populated using the Employees table so there won't be any data type issues.
Below the list box on the search form I have a button called
- butUpdateEmployeeID
When the button is clicked, I want the following to happen:
- The EmployeeCode combo box in the formAcademyRegister be updated with the employeeID as selected in the list in the formsEmployeeSearch form
- The formsEmployeeSearch to be closed
I am comfortable with the process of closing the form, I am however struggling with the code to populate that combo box with the value selected in the list box.
I tried the following:
Me.Form!formAcademyRegister!EmployeeCode = Me!lstEmpSearchResult.Value
But this is not working.
CodePudding user response:
Try the below.
Forms.formAcademyRegister.EmployeeCode.Value = lstEmpSearchResult.Value
DoCmd.Close acForm, Me.Name, acSavePrompt
A couple of notes.
- The keyword
Me
is not required when the code is behind the form itself, but in some situations it makes the code more readable. - The
.Value
property is default property (where applicable) thus can be omitted. It's best practice to keep it for clarity.