Solved

How give user an opportunity to exit a form

Posted on 2013-10-25
1
426 Views
Last Modified: 2013-10-25
I have a form with one required field (in the table setup).  And I have the following code in the onclose event of the form.  But I want to give the user the opportunity to exit the form with no data entry if they want to do so.  Here is my code so far:

Private Sub Form_Close()

    If Nz(Me.txtCompanyName, "") = "" Then
        MsgBox "You must enter a company name!!!"
    Exit Sub
    End If

End Sub

How can I present them with a message box that will either let them stay in the form but return the focus to the txtCompanyName field, OR, let them leave the form with no data entry?
0
Comment
Question by:SteveL13
1 Comment
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 39601632
try using the unload event of the form instead of the close event

Private Sub Form_Unload(Cancel As Integer)
    If Nz(Me.txtCompanyName, "") = "" Then
        If (MsgBox("You must enter a company name!!!" & vbCrLf & "Do you want to continue?", vbYesNo)) = vbYes Then
               Cancel = True
               Me.txtCompanyName.SetFocus
         Else
               Exit Sub
          End If
    End If
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now