Solved

How give user an opportunity to exit a form

Posted on 2013-10-25
1
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

717 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