Link to home
Create AccountLog in
Avatar of mrong
mrong

asked on

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.
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

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
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
Avatar of mrong
mrong

ASKER

The popup message doesn't show up.
Avatar of mrong

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account