View Full Version : Code for populating text fields based on a combobox


boblife42
06-25-2008, 06:11 AM
What I am trying to do is match the Employee Number on a form to a employee number in a table to populate a combobox, the combobox will be populated with the different number of hours that the employee could work in a day, once a number of hours is chosen then the text fields for in and out will be populated. Here is the code I have:
Private Sub cbo_HoursPerDay_AfterUpdate()
If Me.cbo_HoursPerDay.RowSource = "SELECT * FROM tbl_EmployeeShiftInformation WHERE strEmployeeNumber = " & Forms![frm_EmployeeData]!EmployeeNumber Then

Me.InAmTime.Value = Me.cbo_HoursPerDay.Column(2)
Me.OutAmTime.Value = Me.cbo_HoursPerDay.Column(3)
Me.InPmTime.Value = Me.cbo_HoursPerDay.Column(4)
Me.OutPmTime.Value = Me.cbo_HoursPerDay.Column(5)
End If


End Sub

What happens is that all the hour choice for all employees populate the combobox and the text fields are not being populated. I would be grateful for any help.

Thank you

Call_Me_Sam
06-26-2008, 12:07 AM
If Me.cbo_HoursPerDay.RowSource = "SELECT * FROM tbl_EmployeeShiftInformation WHERE strEmployeeNumber = " & Forms![frm_EmployeeData]!EmployeeNumber Then



Hi, i'm slightly confused as to why you are testing in code if the rowsource = the employeenumber. Would it not be easier to have the rowsource of the combobox to requery each time you select a different employee number. Therefore only hours workable would be displayed immediately with in the combobox and then use the code to populate the text boxes when the user clicks/updates the combo box? ask if i'm typing a bit woolly

boblife42
06-26-2008, 04:52 AM
Hi,
Because each employee can possibly work different hours and schedules.
I did figure out a way to do this by adding a combo box for employee number and populating the hour combo box which was used to populate the text fields, I am working on a way to test that the employee numbers match so that the information is not saved wrong. Thanks for the help.