Before we get to the code, you need to make two changes to the form. First, you have Data Entry set to Yes. Open your form in Form View. Notice that your navigation bar is displaying "Record 1 of 1", and the record data is blank. You're positioned to add a new record, and none of the existing records are appearing in your recordset. (This was why the code I gave you before didn't work--there were no records for
any employee, let alone for the one you selected in the combo box, so you always got the "No training records for this employee" message.) With Data Entry set to Yes, you're telling Access that you
only want to use the form to add new records, not to display or edit existing records. You need to set it to No. Allow Additions is set to Yes, so you can still use the form to add records.
The other change is that you need to add a text box and set its Control Source to EmployeeID. Every new record must have an employee ID, and the only place it can come from is the form, so this is required. Make the text box invisible (set Visible to No), because the ID doesn't mean anything to the user, so there's no point in displaying it to them.
Here are the tasks we need to do with VBA code:
1. When the form opens, set cboEmployeeName to the first employee name, and set the Filter property to filter records for that employee. We do this in the Form_Open event procedure.
2. When the user selects and employee, set the Filter property to filter records for that employee. We do this in the cboEmployeeName_AfterUpdate event procedure.
3. When the user attempts to erase the combo box, don't allow it. We do this in the cboEmployeeName_BeforeUpdate event procedure.
4. When the user attempts to change the filtering, explain that they mustn't do this and restore the filtering to its previous value. We do this in the Form_ApplyFilter event procedure.
5. When the user adds a new record, set txtEmployeeID to the EmployeeID value for the current employee in the combo box. We do this in the Form_BeforeInsert event procedure.
Here are the event procedures:
Code:
Private Sub cboEmployeeName_AfterUpdate()
' If the user had chosen Data Entry from the menu, turn it off now.
Me.DataEntry = False
Me.Filter = "EmployeeID = " & Me!cboEmployeeName
Me.FilterOn = True
End Sub
Private Sub cboEmployeeName_BeforeUpdate(Cancel As Integer)
If IsNull(Me!cboEmployeeName) Then
' Uncomment the following lines to explain to the user that
' they can't do this.
'Beep
'MsgBox "Please select an employee."
Cancel = True
End If
End Sub
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
' If the user has chosen Remove Filter/Sort or Apply Filter/Sort
' to turn off the Data Entry mode, do that for them.
If Me.DataEntry _
And (ApplyType = acShowAllRecords Or ApplyType = acApplyFilter) Then
Me.DataEntry = False
Me.FilterOn = True
Cancel = True
Exit Sub
End If
Beep
MsgBox "Sorry, but you can't change filtering on this form."
Cancel = True
End Sub
Private Sub Form_BeforeInsert(Cancel As Integer)
Me!txtEmployeeID = Me!cboEmployeeName
End Sub
Private Sub Form_Filter(Cancel As Integer, FilterType As Integer)
Beep
MsgBox "Sorry, but you can't use filtering on this form."
Cancel = True
End Sub
Private Sub Form_Open(Cancel As Integer)
cboEmployeeName = Me!EmployeeID
Me.Filter = "EmployeeID = " & Me!EmployeeID
Me.FilterOn = True
End Sub
Try this out, and see whether the form's behavior seems reasonable and intuitive to you. If you don't like it, and want to be able to show all records and/or use filtering, let me know and I'll give you an idea on how to modify your form's design.
One more comment: Your form's code module doesn't have Option Explicit at the top. Option Explicit tells VBA that it should give you an error message when you compile the module, if the code in the module attempts to use a variable that isn't declared in a Dim statement. Without Option Explicit, if you misspell a variable name, VBA will take it to be a new variable, not the one you intended. This causes a bug in your program which can be very difficult to find. Most professional programmers always use Option Explicit. You can ensure that every module you create will include Option Explicit by choosing Tools>Options from the VBA editor menu, selecting the Editor tab, and putting a check mark by Require Variable Declaration. I strongly recommend this. Rick Sprague