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
The BeforeUpdate event of the form is the LAST event to run before a record gets saved. The others have been telling you that this is the event you need to use in order to keep Access from saving bad data.So if I start a new record entry and PDate is filled but I scroll up and set focus to a prior record. The New record should be cancelled and undo the data entry for PDate or whatever fields were filled.
Did you implement the code suggested by @MajP ?If I press Esc to undo the record entry, it closes no problem, but as long as a field is populated I get the error before close. Can the form automatically clear the partial record entry then close without prompting "You can't save this at this time"?
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?
Public Function EnsureNotEmpty(frm As Form) As Boolean
Dim ctl As Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox, acOptionGroup
If Instr(ctl.Tag, "Required") > 0 Then
If ctl & "" = "" Then
ctl.SetFocus
EnsureNotEmpty = False
MsgBox ctl.Name & " is required.", vbOKOnly
Exit Function
End If
End If
Case Else
End Select
Next ctl
EnsureNotEmpty = True
End Function
Public Function EnsureNotEmptyAll(frm As Form) As Boolean '''' not tested
Dim ctl As Control
Dim strMsg As String
For Each ctl In frm.Controls
Select Case ctl.ConitrolType
Case acTextBox, acComboBox, acListBox, acCheckBox, acOptionGroup
If ctl & "" = "" Then
ctl.BorderColor = RGB(186, 20, 25)
strMsg = strMsg & ctl.Name & " is required." & vbCrLf
Else
ctl.BorderColor = RGB(192, 192, 192)
End If
Case Else
End Select
Next ctl
If strMsg = "" Then
EnsureNotEmptyAll = True
Else
EnsureNotEmptyAll = False
MsgBox strMsg, vbOKOnly
End If
End Function