data validation in the form

A

Associates

Hi,

I would like to perform data validation before the insertion of a new record
into the table in Access database.

So in the form, i have a few fields that need to be checked to see if they
are empty or entered. I have the following codes in the event of
"Form_BeforeUpdate"

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim mesg, Title As String

'Check if all the required fields are entered
If Me.Stud_ID <> "" Then
If Me.Department_ID <> "" Then
'warning message
mesg = "Data has been changed in this record. Do you want
to proceed?"
Title = "Warning !!!"

If MsgBox(mesg, vbYesNo, Title) = vbNo Then
'cancel the update action
Cancel = True
'undo all the data as well as the beforeupdate event
Me.Undo
End If
End If
End If
End Sub

This code behaves the way i want it to but still the record gets inserted
into the table even when the department_ID field is empty. This is not right.
How do i get around this problem from the form level?

Thank you in advance
 
S

Steve Schapel

Associates,

Your code ios testing the Stud_ID and Department_ID field for the
presence of a zero-length string "" which is very unlikely. Not only
that, but it seems to be backwards, i.e. I assume you do not want to
cancel the update if there is data in these fields. Try it like this:

If IsNull(Me.Stud_ID) Then
If IsNull(Me.Department_ID) Then

.... or, possibley simpler:
If IsNull(Me.Stud_ID) Or IsNull(Me.Department_ID) Then

(Sorry, I am not 100% clear of what you really want to happen, but
hopefully that will help you re-work it)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top