473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Handling Unload Event

lotus18
866 512MB
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
5 2280
Mohan Krishna
115 100+
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
1,977 1GB
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
866 512MB
[Thinking] Setting the form to invisible rather than unloading, I think this is not a better solution.
Nov 30 '07 #4
hariharanmca
1,977 1GB
Good, you can do that.
Nov 30 '07 #5
Killer42
8,435 Expert 8TB
VACEPROGRAMER, I've split your stuff to a new thread, as it is completely unrelated to the original question.
Dec 13 '07 #6

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

Similar topics

5
by: Harry J. Smith | last post by:
I have written a Visual Basic program that does a long calculation and writes the results to disk as it runs. If I click the Close button the window closes but the program keeps running. How can I...
1
by: sidd | last post by:
Hi all, how do i exit out(unload) of a windows service in case of an error. so i have a code block as follows try { } catch(exception ex) { //log to event log
1
by: David A. Beck | last post by:
I have a frameset with an index frame and a main frame. The aspx pages in the main frame are loaded based on the hyperlinks clicked in the index frame. In any aspx page in the main frame I want to...
1
by: hal | last post by:
I have an application that includes an activex component that consumes resources that must be released when the a page is unloaded. Toward this end I subscribe to the unload event of the body...
1
by: Hal | last post by:
My most sincere gratitude to anyone who can help me work around this! I have work that needs to be done in javascript on the client whenever a page is unloaded. To this end, I subscribe to...
0
by: bburkhart | last post by:
I am running into a issue catching a thrown error in the Control.Unload event. Is it possible to throw an Application error from the Unload event and have the global.asax Application_Error catch it?...
6
by: Mike | last post by:
I have a web form in my application that will be used for both viewing and updating information. I have a requirement that if any data has been changed on the page and the user attempts to leave...
3
by: Gauthier Segay | last post by:
Hello, I've an application where all my pages implement a PAGE_CODE string property, this property is stored in HttpContext.Current.Items. In some page, I must persist data in session while...
5
by: =?Utf-8?B?U3RldmVuIFRhbmc=?= | last post by:
It seems that one page XBAP whose Unloaded event never get called, I need put some clearing stuff (I.G stop dispatcher time) when user close browser, it unload event doesn't work, where shall I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.