[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Required Field - form closes

I have this on my form close event:

Private Sub Form_Close()

If IsNull(Me!WhiteBoard_All.Form!Side) Then
    MsgBox "You must select a Side", vbOKOnly, "Missing Data"
    Me!WhiteBoard_All.Form!Side.SetFocus
    Cancel = True
End If

End Sub

It opens a msgbox and when I click OK, the form closes.  Shouldn't the form stay open?  I can't figure it out.
0
Scotto123
Asked:
Scotto123
  • 6
  • 4
  • 3
  • +1
1 Solution
 
ktromp1Commented:
you need a cancel event args perhaps

this is working code. So try building off of this.
   Private Sub Form1_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
      ' Determine if text has changed in the textbox by comparing to original text.
      If textBox1.Text <> strMyOriginalText Then
         ' Display a MsgBox asking the user to save changes or abort.
         If MessageBox.Show("Do you want to save changes to your text?", "My Application", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ' Cancel the Closing event from closing the form.
            e.Cancel = True
         End If ' Call method to save file...
      End If
   End Sub 'Form1_Closing
End Class 'Form1

Open in new window

0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
The Close event does not have a Cancel arg.

They UnLoad event does ...

mx
0
 
NorieCommented:
Try using the Unload event instead of Close.
Private Sub Form_Unload(Cancel As Integer)
   If IsNull(Me!WhiteBoard_All.Form!Side) Then
      MsgBox "You must select a Side", vbOKOnly, "Missing Data"
      Me!WhiteBoard_All.Form!Side.SetFocus
      Cancel = True
   End If

End Sub

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Private Sub Form_UnLoad(Cancel As Integer)

If IsNull(Me!WhiteBoard_All.Form!Side) Then
    MsgBox "You must select a Side", vbOKOnly, "Missing Data"
    Me!WhiteBoard_All.Form!Side.SetFocus
    Cancel = True
End If

End Sub

Maybe the above ...
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Another issue is ... the UnLoad and Close events occur *after* the record has been saved ...

mx
0
 
Scotto123Author Commented:
First solution worked well.
0
 
ktromp1Commented:
@DatabaseMX

http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx
if you read this directly from the Microsoft website. They will tell you that there is such a thing as the CancelEventArgs within the FormClosing event. That is because the FormClosingEventArgs has a Cancel property which inherits the CancelEventArgs.
0
 
ktromp1Commented:
Also, if the first solution worked well, then why was the second one accepted as the answer?
0
 
Scotto123Author Commented:
OnUnLoad was simpler and worked for me.
0
 
Scotto123Author Commented:
Oh, my mistake.  The 2nd solution was the working one.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
RE:
'Private Sub Form1_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

Are you in VB.Net?  Because no such thing exists in Access.


"FormClosingEventArgs Class
.NET Framework 4.5"
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"OnUnLoad was simpler and worked for me. "
As I noted at http:#a38297306

Also, your original code would not Compile, because of Cancel=True in the Close event.

mx
0
 
ktromp1Commented:
That's why I put e.Cancel = True, rather than just Cancel = True.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
This comment "Also, your original code would not Compile, because of Cancel=True in the Close event."

was meant for Scotto123

@ktromp1
Your VB.Net code is not going to work in Access ...

mx
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now