I have a rather strange problem.
I have a form displaying employee details. On this is a subform with details about what training courses an employee has been on.
On the AfterInsert event of the subform, I have some code that asks the user if they would like to add the training course just entered, to a table which lists what job title suits what training course.
If the answer is yes, a further form loads which has a combo box allowing the user to select the importance of this course for this job title.
There's a button on this form, which when pressed, adds the job title, course code and importance to a table.
To be able to refer to the correct course code, in the subform's AfterInsert event, I set a global variable to be equal to the course code, I ten refer to this variable on the OnClick event code in the form that loads.
The problem is, my code never works the first time the main form is loaded. The first time round, when I refer to the global variable, I get a zero length string and an error occurs. However when I add further courses, the code runs fine.
I'll post my code below, cheers for any light that anyone can shed on this!
Pete
'After a new record is inserted into EmployeeCourses
Private Sub Form_AfterInsert()
On Error GoTo Err_Form_AfterInsert
'JTitle - Used to represent the Job Title of the employee who
'has just had a course added.
Dim JTitle As String
'Set the global variable CourseCode to the current course code
'so it can be accessed from frmImportancePicker.
CourseCode = Me.Course_Code.Value
JTitle = Form_frmEmployees.Job_Title.Value
If MsgBox("Would you like to make the course " & Chr(13) & _
CourseCode & Chr(13) & " a suitable course for the job title " _
& JTitle & "?", 36, "Leo"
= vbYes Then
DoCmd.OpenForm "frmImportancePicker"
End If
Exit_Form_AfterInsert:
Exit Sub
Err_Form_AfterInsert:
If Not (Err.Number = 3022) Then
MsgBox Err.Number & ":" & Err.Description & Chr(13) & Chr(13) _
& "If this problem persists, please refer to User Manual" & _
Chr(13) & "for a Troubleshooting contact.", vbExclamation, "Error"
End If
Resume Exit_Form_AfterInsert
End Sub
Private Sub frmImportancePickerContButton_Click()
On Error GoTo Err_frmImportancePickerContButton_Click
Dim db As DAO.Database, rs As DAO.Recordset
'CCode - Used to represent the inserted course's Course Code.
'JTitle - Used to represent the Job Title of the employee who
' has just had a course added.
'Importance - Used to represent the importance the course has
' for this job title.
Dim CCode As Form, JTitle As String, Importance As String
Set db = CurrentDb
'Set the record set to represent tblCoursesSuitableFor
Set rs = db.OpenRecordset("tblCourseSuitableFor"
Set CCode = Form_frmEmployeeCourses
JTitle = Form_frmEmployees.Job_Title.Value
Importance = Me.Importance
'With the defined recordset, add a new record
'with the values held in CCode, JTitle and Importance.
With rs
.AddNew
.Fields("Course Code"
= CCode.CourseCode
.Fields("Job Title"
= JTitle
.Fields("Importance"
= Importance
.Update
End With
MsgBox JTitle & " has been added as a suitable job " _
& "title for this course.", , "Job Title Added"
DoCmd.Close
Exit_frmImportancePickerContButton_Click:
Exit Sub
Err_frmImportancePickerContButton_Click:
MsgBox Err.Description
Resume Exit_frmImportancePickerContButton_Click
End Sub
I have a form displaying employee details. On this is a subform with details about what training courses an employee has been on.
On the AfterInsert event of the subform, I have some code that asks the user if they would like to add the training course just entered, to a table which lists what job title suits what training course.
If the answer is yes, a further form loads which has a combo box allowing the user to select the importance of this course for this job title.
There's a button on this form, which when pressed, adds the job title, course code and importance to a table.
To be able to refer to the correct course code, in the subform's AfterInsert event, I set a global variable to be equal to the course code, I ten refer to this variable on the OnClick event code in the form that loads.
The problem is, my code never works the first time the main form is loaded. The first time round, when I refer to the global variable, I get a zero length string and an error occurs. However when I add further courses, the code runs fine.
I'll post my code below, cheers for any light that anyone can shed on this!
Pete
'After a new record is inserted into EmployeeCourses
Private Sub Form_AfterInsert()
On Error GoTo Err_Form_AfterInsert
'JTitle - Used to represent the Job Title of the employee who
'has just had a course added.
Dim JTitle As String
'Set the global variable CourseCode to the current course code
'so it can be accessed from frmImportancePicker.
CourseCode = Me.Course_Code.Value
JTitle = Form_frmEmployees.Job_Title.Value
If MsgBox("Would you like to make the course " & Chr(13) & _
CourseCode & Chr(13) & " a suitable course for the job title " _
& JTitle & "?", 36, "Leo"
DoCmd.OpenForm "frmImportancePicker"
End If
Exit_Form_AfterInsert:
Exit Sub
Err_Form_AfterInsert:
If Not (Err.Number = 3022) Then
MsgBox Err.Number & ":" & Err.Description & Chr(13) & Chr(13) _
& "If this problem persists, please refer to User Manual" & _
Chr(13) & "for a Troubleshooting contact.", vbExclamation, "Error"
End If
Resume Exit_Form_AfterInsert
End Sub
Private Sub frmImportancePickerContButton_Click()
On Error GoTo Err_frmImportancePickerContButton_Click
Dim db As DAO.Database, rs As DAO.Recordset
'CCode - Used to represent the inserted course's Course Code.
'JTitle - Used to represent the Job Title of the employee who
' has just had a course added.
'Importance - Used to represent the importance the course has
' for this job title.
Dim CCode As Form, JTitle As String, Importance As String
Set db = CurrentDb
'Set the record set to represent tblCoursesSuitableFor
Set rs = db.OpenRecordset("tblCourseSuitableFor"
Set CCode = Form_frmEmployeeCourses
JTitle = Form_frmEmployees.Job_Title.Value
Importance = Me.Importance
'With the defined recordset, add a new record
'with the values held in CCode, JTitle and Importance.
With rs
.AddNew
.Fields("Course Code"
.Fields("Job Title"
.Fields("Importance"
.Update
End With
MsgBox JTitle & " has been added as a suitable job " _
& "title for this course.", , "Job Title Added"
DoCmd.Close
Exit_frmImportancePickerContButton_Click:
Exit Sub
Err_frmImportancePickerContButton_Click:
MsgBox Err.Description
Resume Exit_frmImportancePickerContButton_Click
End Sub