Popup message before saving record in Access 2013 Form

Greeting,

I have a field named Company_Name on my Access 2013 form. I want to add a event on the form. If Company_Name is blank when the users try to add/save the data, it will popup a message say "Company Name is required". The data won't be saved untill Company_Name if filled out.

Please provide sample code.

Thanks  in advance.
mrongAsked:
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.

Jeffrey CoachmanMIS LiasonCommented:
On the before Update event of the form try something like this:

If me.Company_Name="" Then
    msgbox "Company name cannot be left blank."
    me.Company_Name.setfocus
    cancel=True
End if
0
Jeffrey CoachmanMIS LiasonCommented:
Sorry, this is better:

If IsNull(Me.Company_Name) Then
    MsgBox "Company name cannot be left blank."
    Me.Company_Name.SetFocus
    Cancel = True
End If
0
mrongAuthor Commented:
The popup message doesn't show up.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mrongAuthor Commented:
ok, the popup shows up.

I have a save button on the form with the following code. If the user leaves Company_Name blank, it will popup the message then just close the form itself. Is there anyway to have the form stays unless user close the form?

DoCmd.Close
0
Jeffrey CoachmanMIS LiasonCommented:
The presence of this button was not mentioned in your original post...

What you have there is not really a "Save" button, the code you have there Closes the form.
So this is really a "Close Button"

You button will force the form to close regardless..

If it were me, I would delete this button and tell users to use the standard "X" in the upper right hand of the form.
The standard "X" will allow the "validation" code to run as you specify.

JeffCoachman
0
Jeffrey CoachmanMIS LiasonCommented:
Just to be clear, ..there are odd things that happen when you use Docmd.Close, ..see here:
http://allenbrowne.com/bug-01.html

It in that link it notes that you can get around this by using a "Macro" on the button (instead of VBA) to close the form.

So try this:

Action: Close Window
Object Type: Form
Object Name: YourFormName
Save: Prompt  (Not this is only applicable to save design changes to the form, ...not data changes as you are wanting)
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
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.