By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,592 Members | 2,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,592 IT Pros & Developers. It's quick & easy.

Handling Unload Event

lotus18
100+
P: 866
Hello World

This is a follow-up question from http://www.thescripts.com/forum/thread742864.html for the reason that Killer42 and Mohan wants me to start a new thread... so this is it.

The question... can you handle (form) unload event without prompting a message box but to be able to prompt a message box to the user if he wants to save before closing ... instead of just closing the window -Kabyr.

Here's the sample code:

Where Text1.Text is generated

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2.     If Text2.Text = "" Then
  3.         MsgBox "Please type in course title.", 48
  4.         Text2.SetFocus
  5.         Exit Sub
  6.     ElseIf Text3.Text = "" Then
  7.         MsgBox "Please type in the description of the course.", 48
  8.         Text3.Text.SetFocus
  9.         Exit Sub
  10.     End If
  11.  
  12.     SetConnection 'My database connection
  13.     CheckConnection rs 'Checks whether the recordset is open/close
  14.     Screen.MousePointer = vbHourglass
  15.  
  16.     If AddCourse = True Then
  17.     'Add new course
  18.          rs.Open "Insert Into Courses ([CourseID],[Title],[Description]) Values " _
  19.          & "('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "')", con, 3, 3
  20.          MsgBox "New course has been successfully added.", 64
  21.          Unload Me
  22.     Else
  23.     'Update course
  24.          rs.Open "Update Courses Set [Title]='" & Text2.Text & "', [Description]='" & _
  25.          Text3.Text & "' Where CourseID='" & Text1.Text & "'", con, 3, 3
  26.          MsgBox "Course has been successfully updated.", 64
  27.          Unload Me
  28.     End If
  29.     Screen.MousePointer = vbDefault
  30. -------------------------------------------------------------------------------
  31. Private Sub Form_Unload(Cancel As Integer)
  32.      If MsgBox("Do you want to save?", 32 + 4) = 6 Then
  33.           Cancel = 1
  34.      End If
  35. End Sub
  36.  
  37.  
Nov 30 '07 #1
Share this Question
Share on Google+
5 Replies


Mohan Krishna
100+
P: 115
This is what really the problem in that thread...
http://www.thescripts.com/forum/thread742864.html
But it depends on the form and/or type of application. Let us say for an instance we have 1 form and containing all the list of any record, then when adding/modifying a particular record we're using another form and it is shown as vbmodal. What if the user add new record, how can you unload that form without prompting a message box? : )
So how to rectify this when MODAL forms are shown.
Nov 30 '07 #2

hariharanmca
100+
P: 1,977
Use this logic when you are using edit command button in your form, other wise it will popup even any keypress in your form controls.
Nov 30 '07 #3

lotus18
100+
P: 866
[Thinking] Setting the form to invisible rather than unloading, I think this is not a better solution.
Nov 30 '07 #4

hariharanmca
100+
P: 1,977
Good, you can do that.
Nov 30 '07 #5

Expert 5K+
P: 8,434
VACEPROGRAMER, I've split your stuff to a new thread, as it is completely unrelated to the original question.
Dec 13 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.