Solved

How give user an opportunity to exit a form

Posted on 2013-10-25
1
417 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 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

11 Experts available now in Live!

Get 1:1 Help Now