How to force a field not to be left blank

I have a form that requires an unbound field not to be null, but the data it contains doesn't have to be there in the first place, ie. I can't just make the data a required field in its table.

So I wrote a little bit of VBA to do it ...

Private Sub newlabel_LostFocus()
If IsNull([newlabel]) Then
MsgBox("Content Required")
Me!newlabel.SetFocus
End If
End Sub

This works OK, in that the message pops up under the right conditions (eg. if the user deletes existing content, or leaves a blank entry blank), but the focus moves on to the next field for some reason. I can correct this by adding a SendKeys backtab, but I don't like to use SendKeys and can't see what's wrong with the code.
pauldownhamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeTooleCommented:
Use the Before Update event on your text box to make sure that the user doesn't update it to Blank/Null:

Private Sub TheControl_BeforeUpdate(Cancel As Integer)
    If Nz(Me.TheControl, "") = "" Then
        MsgBox "Field must have a value"
        Cancel = True
    End If
End Sub

The focus will stay with the control till the user enters a value.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
c_popCommented:
Hi,

Maybe you can use
IsNull([newlabel]) or IsEmpty([newlabel])
0
pauldownhamAuthor Commented:
works a treat ... thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.