A family of Microsoft relational database management systems designed for ease of use.
Sorry for the delayed reply; I was busy.
I would not do it the way that is suggested at that link, because I think it better not to let the user into the subform at all if the required info on the parent form is not filled out. This problem boils down to two simple requirements:
- Don't let a record be saved on main form or subform if required fields are not filled out.
- Don't let the user into a subform at all unless the parent form has been filled out.
If you take care of (1), then (2) is a matter of using the Enter event of the subform control (to see if the form that contains that control is on a new record. If it is, then warn the user and set the focus back to the appropriate control. You don't have to check the individual controls, because requirement (1) will prevent the record from being saved, and automatically keep the focus from going to the subform if the parent form has only been partially filled out.
I have downloaded your database, and find the following modifications handle the situation. First, put the following utility function into a standard module:
'------ start of code ------
Function fncRequiredFieldsMissing(frm As Form) As Boolean
' Check form (passed as <frm> for controls marked as "Required"
' in their Tag properties. If any required control is empty, return
' True, display an error message listing all the "missing" fields,
' and set the focus to the first missing control (where "first" is
' based on the controls' tab order). In the list of missing controls,
' use each control's caption if possible, else use the name of the
' control after stripping off the expected object-type prefix (if
' present).
'
' Copyright (c) 2013, Dirk Goldgar. You may use this code freely in your
' applications, provided that the copyright notice remains intact.
On Error Resume Next
Dim ctl As Access.Control
Dim strErrCtlName As String
Dim strErrorMessage As String
Dim strMsgName As String
Dim lngErrCtlTabIndex As Long
Dim blnNoValue As Boolean
lngErrCtlTabIndex = 99999999 'more than max #controls
For Each ctl In frm.Controls
With ctl
Select Case .ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If .Tag = "Required" Then
blnNoValue = False
If IsNull(.Value) Then
blnNoValue = True
Else
If .ControlType = acTextBox Then
If Len(.Value) = 0 Then
blnNoValue = True
End If
End If
End If
If blnNoValue Then
strMsgName = vbNullString
If .Controls.Count = 1 Then
strMsgName = .Controls(0).Caption
If Right$(strMsgName, 1) = ":" Then
strMsgName = Trim$(Left$(strMsgName, Len(strMsgName) - 1))
End If
End If
If Len(strMsgName) = 0 Then
strMsgName = .Name
Select Case Left$(strMsgName, 3)
Case "txt", "cbo", "lst", "chk"
strMsgName = Mid(strMsgName, 4)
End Select
End If
strErrorMessage = strErrorMessage & vbCr & _
" " & strMsgName
If .TabIndex < lngErrCtlTabIndex Then
strErrCtlName = .Name
lngErrCtlTabIndex = .TabIndex
End If
End If
End If
Case Else
' Ignore this control
End Select
End With
Next ctl
If Len(strErrorMessage) > 0 Then
MsgBox "The following fields are required:" & vbCr & _
strErrorMessage, _
vbInformation, "Required Fields Are Missing"
frm.Controls(strErrCtlName).SetFocus
fncRequiredFieldsMissing = True
Else
fncRequiredFieldsMissing = False
End If
End Function
'------ end of code ------
Next, on each form and subform, select the controls that are required, and set their .Tag properties (on the Other tab of the property sheet) to "Required" (without the quotes).
Next, create this BeforeUpdate event procedure in *both* frmShiftDay and frmShiftMachinesSubform:
'------ start of code ------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = fncRequiredFieldsMissing(Me)
End Sub
'------ end of code ------
Next, create this event procedure for the Enter event of the subform control. frmShiftMachinesSubform, on frmShiftDay:
'------ start of code ------
Private Sub frmShiftMachinesSubform_Enter()
If Me.NewRecord Then
MsgBox _
"You must fill in the shift information before entering machine details.", _
vbInformation, _
"Shift Info Required"
Me.txtShiftDate.SetFocus
End If
End Sub
'------ end of code ------
Next, create this event procedure for the Enter event of the subform control. frmMachineOutputSubform, on frmShiftMachinesSubform:
'------ start of code ------
Private Sub frmMachineOutputSubform_Enter()
If Me.NewRecord Then
MsgBox _
"You must fill in the machine information before entering product details.", _
vbExclamation, _
"Machine Info Required"
Me.txtMachineID.SetFocus
End If
End Sub
'------ end of code ------
Also, add this to the Current event procedure of frmShiftDay:
'------ start of code ------
If Me.NewRecord Then
Me.cboShift.SetFocus
End If
'------ end of code ------
And add this to the Current event procedure of frmShiftMachinesSubform:
'------ start of code ------
If Me.NewRecord Then
Me.txtMachineID.SetFocus
End If
'------ end of code ------
Finally, assuming this works for you -- as it works for me -- remove the code you have in the BeforeInsert events of the subforms; you don't need it any more.