View Full Version : Problem with code


boblife42
06-19-2008, 10:44 AM
I have a combo box with three choices from a value list, 8 Hours, 10 Hours, or Custom. I have one table that has the following fields, EmployeeNumber, 8HourInAm, 8HourOutAM, 8HourInPM, 8HourOutPM, 10HourInAm, 10HourOutAM, 10HourInPM, 10HourOutPM, CustomInAm, CustomOutAM, CustomInPM, CustomOutPM.

What I was trying to do is when the user selects 8 hours in the combo box on the form the time entered in the table for 8HourInAm, 8HourOutAM, 8HourInPM, 8HourOutPM is entered in the InAmTime, OutAmTime, InPmTime, OutPmTime fields on the form, for that specific user.
Here is the code I have so far:
Private Sub cbo_HoursPerDay_AfterUpdate()

If cbo_HoursPerDay.RowSource = "8 Hours" Then
Me.InAmTime.Value = tbl_EmployeeShiftInformation.['8HourInAM']
Me.OutAmTime.Value = tbl_EmployeeShiftInformation.['8HourOutAM']
Me.InPmTime.Value = tbl_EmployeeShiftInformation.['8HourInPM']
Me.OutPmTime.Value = tbl_EmployeeShiftInformation.['8HourOutPM']
End If
If cbo_HoursPerDay.RowSource = "10 Hours" Then
Me.InAmTime.Value = tbl_EmployeeShiftInformation.['10HourInAM']
Me.OutAmTime.Value = tbl_EmployeeShiftInformation.['10HourOutAM']
Me.InPmTime.Value = tbl_EmployeeShiftInformation.['10HourInPM']
Me.OutPmTime.Value = tbl_EmployeeShiftInformation.['10HourOutPM']
End If
If cbo_HoursPerDay.RowSource = "Custom" Then
Me.InAmTime.Value = tbl_EmployeeShiftInformation.['CustomInAM']
Me.OutAmTime.Value = tbl_EmployeeShiftInformation.['CustomOutAM']
Me.InPmTime.Value = tbl_EmployeeShiftInformation.['CustomInPM']
Me.OutPmTime.Value = tbl_EmployeeShiftInformation.['CustomOutPM']
End If

End Sub
____________________________________
I am sure I need a lookup to match the employee number on the form to the employee number in the table. Thank you for any help that you can give, I am relatively new at VBA code.

pbaldy
06-19-2008, 11:33 AM
If it were one I'd use a DLookup, but for 4 I'd use a recordset:

Dim strSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb()

strSQL = "SELECT * FROM TableName WHERE EmployeeNumber = " & Me.EmployeeNumber

Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

'Your code here, using this to get data from the recordset:
Me.ControlName = rs!FieldName

set rs = nothing
set db = nothing

boblife42
06-19-2008, 01:05 PM
Thank you that should work, is the rest of the code okay.

pbaldy
06-19-2008, 01:16 PM
Actually no. Drop the ".RowSource" from the end of the combo box references (.Value is the default, which is what you want). I'm not entirely comfortable with what might be an un-normalized design, but I'm not sure there's enough there to say either way, which is why I didn't say anything to start with.

boblife42
06-19-2008, 01:18 PM
Thank you I will do that.