Message text

DAW

Registered User.
Local time
Today, 04:56
Joined
Mar 22, 2006
Messages
70
I have required fields on my form. If the user doesn't enter all values they get the unhelpful "The field xxx cannot contain a Null value because..." which is correct, but I'd like to give a more meaningful message. Can this be done?
 
I like to use the form's Before Update evnt to do the data validation.

Here is an example using the Customer form in the Northwind example in Access 2000:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

   Cancel = False


' perform data validation
If IsNull(Me.CompanyName) Then

   MsgBox "You must enter a Company Name.", vbCritical, "Data entry error..."
   Me.CompanyName.BorderColor = vbRed
   DoCmd.GoToControl "CompanyName"
      
   Cancel = True

End If


If Not Cancel Then
  ' passed the validation process

    If Me.NewRecord Then
        If MsgBox("Data will be saved, Are you Sure?", vbYesNo, "Confirm") = vbNo Then
            Cancel = True
        Else
            ' run code for new record before saving
        
        End If
    
    
    Else
        If MsgBox("Data will be modified, Are you Sure?", vbYesNo, "Confirm") = vbNo Then
            Cancel = True
        Else
           ' run code before an existing record is saved
           ' example: update date last modified
            
        End If
    End If

End If


' if the save has been canceled or did not pass the validation , then ask to Undo changes
If Cancel Then

    If MsgBox("Do you want to undo all changes?", vbYesNo, "Confirm") = vbYes Then
        Me.Undo

    End If
    
End If



End Sub
 
or look at error handling, and manage the error messages

the error you received can be intercepted, and handled more gracefully

most developers will use both these techniques, depending on the situation (ie error handling, and pre-validating)
 

Users who are viewing this thread

Back
Top Bottom