There are 4 fields.The other complication is what you mean by "incomplete fields". Are all fields required, some fields required? What constitutes an incomplete field in this scenario? Is it ever permissible to leave a field unchanged at this point? Does this requirement only apply sometimes, i.e. when the user wants to select an existing record before saving the current record?
Private Sub Form_BeforeUpdate(Cancel As Integer)
DoCmd.SetWarnings False
'PDate -Required
'Item -Required
'QUANTITY -Required and not zero
'Cost -Required and not zero
Dim strMsg
Dim invalid As Boolean
strMsg = "The Following Field/s Need to be entered:" & vbCrLf
If IsNull(Me.UPC) Then
strMsg = strMsg & "UPC" & vbCrLf
invalid = True
End If
If Not IsDate(Me.PurDate) Then
strMsg = strMsg & "Purchase Date" & vbCrLf
invalid = True
End If
If Me.QUANTITY <= 0 Then
strMsg = strMsg & "Quantity" & vbCrLf
invalid = True
End If
If Me.Cost <= 0 Then
strMsg = strMsg & "Cost"
invalid = True
End If
If invalid Then
strMsg = strMsg & vbCrLf & vbCrLf & "Please fix or hit escape to clear."
MsgBox strMsg, vbInformation, "Required"
Cancel = True
'Me.Undo
End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not IsValid Then
Cancel = True
Me.Undo
End If
End Sub
A more elegant (if I may say so) implementation of field level validation of required fields, including optionally required fields (if status=X then the field is required) can be found in the Northwind 2 Dev edition template. Per the documentation, it takes only 3 lines of code to validate a form.Code:Private Sub Form_BeforeUpdate(Cancel As Integer) DoCmd.SetWarnings False 'PDate -Required 'Item -Required 'QUANTITY -Required and not zero 'Cost -Required and not zero Dim strMsg Dim invalid As Boolean strMsg = "The Following Field/s Need to be entered:" & vbCrLf If IsNull(Me.UPC) Then strMsg = strMsg & "UPC" & vbCrLf invalid = True End If If Not IsDate(Me.PurDate) Then strMsg = strMsg & "Purchase Date" & vbCrLf invalid = True End If If Me.QUANTITY <= 0 Then strMsg = strMsg & "Quantity" & vbCrLf invalid = True End If If Me.Cost <= 0 Then strMsg = strMsg & "Cost" invalid = True End If If invalid Then strMsg = strMsg & vbCrLf & vbCrLf & "Please fix or hit escape to clear." MsgBox strMsg, vbInformation, "Required" Cancel = True 'Me.Undo End If End Sub
View attachment 115789
Not OP, but I wish when experts here refer us to northwind2, they could be a little bit more specific. May I ask in which form should I see these 3 lines for validation and see how it's done?(if status=X then the field is required).............it takes only 3 lines of code to validate a form.
Did you follow the link? If so, you would have seen it's about the Orders form, specifically frmOrderDetails.Not OP, but I wish when experts here refer us to northwind2, they could be a little bit more specific. May I ask in which form should I see these 3 lines for validation and see how it's done?
I searched the whole project for "status", 188 lines were found. I's hard to go through all and check what is what.
I searched for "If status=", the result was 0.
In which form's validation I can see how it's done.
Thanks.
Yes, and No. I clicked it, but didn't read the article. I was in my 15 min break time and your link title (Documentation) gave me the idea of a long article.Did you follow the link?