If Null Fields then dont' save record

P

Perry Kew

I have Access 2000.

I have a books form with several fields. If three fields
(BooksID, AuthorID or Date) are null, I don't want to
have the ability to save the record. I would like to
generate a message box reminding me to fill these fields.

How do I do that?

Thanks for your help.

--Perry
 
M

Marshall Barton

Perry said:
I have Access 2000.

I have a books form with several fields. If three fields
(BooksID, AuthorID or Date) are null, I don't want to
have the ability to save the record. I would like to
generate a message box reminding me to fill these fields.


Use the form's BeforeUpdate event procedure to check the
text boxes. If any are Null, cancel the event:

If IsNull(Me.BooksID) Or IsNull(Me.AuthorID) _
Or IsNull(Me.Date) Then
MsgBox("Fill in the required fields")
Cancel = True
Exit Sub
End If
 
J

JohnC

I like to check for required fields all at once and notify the user which
fields are missing.
In the form's Before Update event, you might try this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate

Dim strReqMsg As String
Dim strValidData As String

strValidData = True

' name of control = field name
' txtBooks = BooksID
' txtAuthor = AuthorID
' txtDate = Date

strReqMsg = "The Following Required Fields Were Left Blank."
If IsNull(txtBooks) Then strReqMsg = strReqMsg & vbCrLf & "Employee
ID"
If IsNull(txtAuthor) Then strReqMsg = strReqMsg & vbCrLf & "Course
Type"
If IsNull(txtDate) Then strReqMsg = strReqMsg & vbCrLf & "Resource
Code"

' The original string strReqMsg is 46 characters

If Len(strReqMsg) > 46 Then
MsgBox strReqMsg
strValidData = False
Cancel = True
End If

If strValidData = False Then
GoTo Exit_Form_BeforeUpdate
End If

Exit_Form_BeforeUpdate:
Exit Sub

Err_Form_BeforeUpdate:
MsgBox Err.Description
Resume Exit_Form_BeforeUpdate

End Sub
 
J

JohnC

<Correction>
I like to check for required fields all at once and notify the user which
fields are missing.
In the form's Before Update event, you might try this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate

Dim strReqMsg As String
Dim strValidData As String

strValidData = True

' name of control = field name
' txtBooks = BooksID
' txtAuthor = AuthorID
' txtDate = Date

strReqMsg = "The Following Required Fields Were Left Blank."
If IsNull(txtBooks) Then strReqMsg = strReqMsg & vbCrLf & "BooksID"
If IsNull(txtAuthor) Then strReqMsg = strReqMsg & vbCrLf &
"AuthorID"
If IsNull(txtDate) Then strReqMsg = strReqMsg & vbCrLf & "Date"

' The original string strReqMsg is 46 characters

If Len(strReqMsg) > 46 Then
MsgBox strReqMsg
strValidData = False
Cancel = True
End If

If strValidData = False Then
GoTo Exit_Form_BeforeUpdate
End If

Exit_Form_BeforeUpdate:
Exit Sub

Err_Form_BeforeUpdate:
MsgBox Err.Description
Resume Exit_Form_BeforeUpdate

End Sub
 

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

Top