J
JudyB
Help needed.
I have created a master form (Employees) with a subform (Service Record).
The Service Record subform has fields ServiceRecordID, EmployeeID,
DepartmentName, JobTitleName, DateStart, DateEnd, and WeeksService. I want
to use the subform to track an employee's service time in each department
he/she works during their time of employment along with the various job
titles that he/she may hold. I have created a query (Service Record Query)
with a field titled
"Status". If the DateEnd field is left blank, "Current" will automatically
fill this field. If DateEnd has a date, the field fills with "Null".
I am currently using the following code to retrieve each employees current
department and job title:
Private Sub Form_Current()
Dim s As String
Dim rs As Recordset
s = "SELECT JobTitleName,DepartmentName" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND Status = 'Current';"
If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s)
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End Sub
It works great for employees that have "Current" in the Status field of the
Service Record Query, but I am getting the following error message for
employees that have "Null" in the Status field.
Run-Time Error '-2147352567 (80020009)': The
value you entered is not valid for this field.
I assume this is because the system is lost when it can't find "Current" in
the Status field? I want to separate the current employees from the
terminated (Null in the Status field) employees for reporting purposes. I
would have thought that the Else statement at the end of the code would have
taken care of the problem, but I am still green and do not have a clear
understanding of code yet.
I was advised to use the following code to solve the problem, but I didn't
have any luck with it either:
Private Sub Form Current()
Dim s As String
Dim rs As Recordset
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
If Not Me.NewRecord Then
If [Status] = "Current" Then
s = "SELECT JobTitleName,DepartmentName" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND Status = 'Current';"
Set rs = CurrentDb.OpenRecordset(s)
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
rs.Close
Set rs = Nothing
End If
End If
Can anyone offer a solution? Any help is greatly appreciated. Thanks
I have created a master form (Employees) with a subform (Service Record).
The Service Record subform has fields ServiceRecordID, EmployeeID,
DepartmentName, JobTitleName, DateStart, DateEnd, and WeeksService. I want
to use the subform to track an employee's service time in each department
he/she works during their time of employment along with the various job
titles that he/she may hold. I have created a query (Service Record Query)
with a field titled
"Status". If the DateEnd field is left blank, "Current" will automatically
fill this field. If DateEnd has a date, the field fills with "Null".
I am currently using the following code to retrieve each employees current
department and job title:
Private Sub Form_Current()
Dim s As String
Dim rs As Recordset
s = "SELECT JobTitleName,DepartmentName" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND Status = 'Current';"
If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s)
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End Sub
It works great for employees that have "Current" in the Status field of the
Service Record Query, but I am getting the following error message for
employees that have "Null" in the Status field.
Run-Time Error '-2147352567 (80020009)': The
value you entered is not valid for this field.
I assume this is because the system is lost when it can't find "Current" in
the Status field? I want to separate the current employees from the
terminated (Null in the Status field) employees for reporting purposes. I
would have thought that the Else statement at the end of the code would have
taken care of the problem, but I am still green and do not have a clear
understanding of code yet.
I was advised to use the following code to solve the problem, but I didn't
have any luck with it either:
Private Sub Form Current()
Dim s As String
Dim rs As Recordset
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
If Not Me.NewRecord Then
If [Status] = "Current" Then
s = "SELECT JobTitleName,DepartmentName" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND Status = 'Current';"
Set rs = CurrentDb.OpenRecordset(s)
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
rs.Close
Set rs = Nothing
End If
End If
Can anyone offer a solution? Any help is greatly appreciated. Thanks