If Then Statement for Null Value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have Access 2003. I need to create code to check to see if my field named
"Contents" is empty and if it is, to prompt the user. I've tried various
ways to handle this, but nothing is working. Below is my latest code which
also doesn't work.

'Check to make sure Contents is not empty
If IsNull([Contents]) = True Then
strMsg = strMsg & "Contents field can't be empty"
Me.Contents.SetFocus 'set the focus to the field
GoTo Err_Cancel_Button
End If

Any assistance would be greatly appreciated.
 
hi,
I have Access 2003. I need to create code to check to see if my field named
"Contents" is empty and if it is, to prompt the user. I've tried various
ways to handle this, but nothing is working. Below is my latest code which
also doesn't work.
The following test covers all possibilities:

If Len(Nz([Field], "") > 0) Then
' Not empty or null
End If


mfG
--> stefan <--
 
The code worked perfectly. Thank you so much!

Stefan Hoffmann said:
hi,
I have Access 2003. I need to create code to check to see if my field named
"Contents" is empty and if it is, to prompt the user. I've tried various
ways to handle this, but nothing is working. Below is my latest code which
also doesn't work.
The following test covers all possibilities:

If Len(Nz([Field], "") > 0) Then
' Not empty or null
End If


mfG
--> stefan <--
 
The code doesn't appear to be incorrect. It may be in the wrong event.
There are two events that are most commonly used to validate user input.
Which you use will depend on how your form is set up. Both are Before Update
events. One at the control level and one at the form level. Both events can
be canceled. You will notice a Cancel As Integer argument in them. If you
set the Cancel to anything other than 0, the update will not be applied.

If you do it at the control level, you don't need to set the focus to the
control, the focus will remain on the control.

Private Sub SomeControl_BeforeUpdate()
If IsNull([Contents]) Then
Cancel = True
strMsg = strMsg & "Contents field can't be empty"
End If
End Sub

I also use a version of Stefan's expressioin"

If Len(Trim(Nz(Me.SomeControl),"")) = 0 Then
 
hi,
I also use a version of Stefan's expressioin"

If Len(Trim(Nz(Me.SomeControl),"")) = 0 Then
Ah, got me. I also use the Trim().


mfG
--> stefan <--
 

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

Back
Top