599CD.com 11/14: Photoshop 102 Handbook Finished   Collapse Menus
 
 
New Feature: Be sure to check out our new Message Forums!
New Releases:   10/20: Access Recordsets,  8/20: Active Server Pages,  7/22: PowerPoint
dismiss
 
   
 

What's New?  |  Courses  |  Theater  |  Demo  |  Tips  |  Blog  |  Forums  |  Search  |  Help  |  Order

 
 
New Tips Added Weekly. Get Notified of New Tips & Tricks.
 
     
 
Tips & Tricks
   

  Access   Excel   Word   Windows   FrontPage   Hardware   Misc

 
 

Microsoft Access MsgBox: Yes or No?

How to get a Yes or No answer from your user.

Q: I would like a button the user has to click on that checks the values in a form and determines whether or not it's OK to close the form. The user must click on my button and can't just shut the form down.

A: First, make yourself a simple form. I have a form here with two fields on it - a name and age. Let's assume for the purposes of this example, the person's age has to be between 21 and 50. If not, the user gets an error.

 

So I'm going to right-click on the Close button in Design View to create a BUILD EVENT with some VBA code in it. Here's what I'm going to say:

Private Sub Command0_Click()

    Dim myReply

    If myAge > 50 Or myAge < 21 Then
        MsgBox ("Age must be between 21 and 50")
        Exit Sub
    End If

    myReply = MsgBox("Are you sure?", vbYesNo)
    If myReply = vbYes Then
        DoCmd.Close
    End If

End Sub

 

The first IF block checks to see if the age that was entered in is valid. If you enter something like 62, you get the error message and you're forced back to the form to correct it.

 

Now, if you get to the second IF block, we can assume your age is correct, and you're asked if you now want to exit the form. If you click on YES, the form exits, saving your record.

 

Now, of course, there's nothing to stop your user from CLOSING the form with the [X] close button. So we need to turn off that button in the Form Properties. I'll also turn off the Control Box and Max/Min buttons while I'm at it.

 

You can also prevent the user from moving from record to record (scrolling ahead or back) by turning off the Navigation Buttons too. I'll also turn off the Record Selectors to prevent them from deleting a record as well.

 

Now we have a form that is bullet-proof. The user HAS to follow our rules. He can't close the form without using our button, and he can't move from record to record or delete anything without using a button that we would have to create.

 

I cover all of these concepts in my various Access tutorials. Form properties such as the Record Selectors, Navigation Buttons, etc. are covered in Access 103. The VBA programming starts in my 300-level classes starting with Access 301.

 

Huge Discounts Available
When you purchase multiple classes together
Huge savings up to 50% off! Order Now.
 

You may want to read these articles from the 599CD Blog:

 

 

Need
Help?

   
Do you have questions about Word, Excel, Access, Web Design, or computers in general? Just ask us anything you'd like.
 

Get Free Tips & Tricks
 

Join our mailing list today and get information on our Free Tips & Tricks Newsletter including free video tutorials, eBooks, live seminars, and more.

Email:
Name:
Type in the word to the left:
 
Your email will be kept 100% safe and will never be given to 3rd parties.
 


CLICK HERE for a FREE lesson



Order your first 599CD course now.
Your Satisfaction is Guaranteed!


Subscribe to our RSS FeedWhat's This?

Subscribe to RSS Feed  Add to Live Bookmarks  Add to My AOL  Add to MyYahoo  Add to Google Reader or Homepage    hide  help  

599CD Home   |   Learn More   |   What's New?   |   Contact Us   |   Free Demo   |   FAQs   |   Order Now   |   Become an Affiliate   |   TechHelp   |   MYOLP   |   Jobs   |   Chat   |   Downloads   |   Handbooks  (Text)   |    Mailing List   |   Lost Passwords   |   Referral Program   |   Online Poll   |   Troubleshooter   |   Corporate / Educational / Government / Non-Profit Sales   |   Message Forums   |   Testimonials   |   Privacy Policy   |   Tips & Tricks