Handling Unload Event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lotus18
    Contributor
    • Nov 2007
    • 865

    Handling Unload Event

    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

    [code=vb]
    Private Sub cmdSave_Click()
    If Text2.Text = "" Then
    MsgBox "Please type in course title.", 48
    Text2.SetFocus
    Exit Sub
    ElseIf Text3.Text = "" Then
    MsgBox "Please type in the description of the course.", 48
    Text3.Text.SetF ocus
    Exit Sub
    End If

    SetConnection 'My database connection
    CheckConnection rs 'Checks whether the recordset is open/close
    Screen.MousePoi nter = vbHourglass

    If AddCourse = True Then
    'Add new course
    rs.Open "Insert Into Courses ([CourseID],[Title],[Description]) Values " _
    & "('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "')", con, 3, 3
    MsgBox "New course has been successfully added.", 64
    Unload Me
    Else
    'Update course
    rs.Open "Update Courses Set [Title]='" & Text2.Text & "', [Description]='" & _
    Text3.Text & "' Where CourseID='" & Text1.Text & "'", con, 3, 3
    MsgBox "Course has been successfully updated.", 64
    Unload Me
    End If
    Screen.MousePoi nter = vbDefault
    -------------------------------------------------------------------------------
    Private Sub Form_Unload(Can cel As Integer)
    If MsgBox("Do you want to save?", 32 + 4) = 6 Then
    Cancel = 1
    End If
    End Sub

    [/code]
  • Mohan Krishna
    New Member
    • Oct 2007
    • 115

    #2
    This is what really the problem in that thread...

    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.

    Comment

    • hariharanmca
      Top Contributor
      • Dec 2006
      • 1977

      #3
      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.

      Comment

      • lotus18
        Contributor
        • Nov 2007
        • 865

        #4
        [Thinking] Setting the form to invisible rather than unloading, I think this is not a better solution.

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Good, you can do that.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            VACEPROGRAMER, I've split your stuff to a new thread, as it is completely unrelated to the original question.
            Last edited by Killer42; Dec 13 '07, 12:24 AM.

            Comment

            Working...