Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Closing out of a form - deleting incomplete record (2000)

    I have created a form that I want to modify so that if a user closes out of the form without filling in the required fields, it will prompt them to either close the form & not save the record or return back to the form. Here is my code:

    Private Sub Form_Close()
    Dim Msg, Style, TITLE, Help, Ctxt, Response, MyString
    Msg = "You have not entered all of the required fields. Do you want to exit?" ' Define message.
    Style = vbYesNo ' Define buttons.
    TITLE = "Incomplete record" ' Define title.
    Dim stDocName As String
    Dim stLinkCriteria As String

    If IsNull(Me.DEPT) = True Or IsNull(Me.ORDER_TYPE) = True Or IsNull(Me.TITLE) = True Or IsNull(Me.AUTHOR) = True Or (IsNull(Me.SERIES_NAME) = True And Me.SERIES = 0) Then
    Response = MsgBox(Msg, Style, TITLE)
    If Response = vbYes Then
    Me.Undo
    ElseIf Response = vbNo Then
    stDocName = "frmLiaisonOrderForm"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    End If
    End If

    End Sub

    Right now this code doesn't do much. It does prompt the user with the message box, but if the user clicks "yes" it will close them out and save the record. If the user clicks "no" then it will close them out of the form instead of return them. Can someone help me to modify this?

    Thanks,
    Kindra

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Closing out of a form - deleting incomplete record (2000)

    You can't cancel a Form_Close, you can only cancel a Form_Unload event, so move your code to that event and set Cancel = True to keep them in the form when the record is incomplete.
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing out of a form - deleting incomplete record (2000)

    I'm getting an error:

    "The expression On Unload you entered as the event property setting produced the following error: Procedure declaration does not match description of event or procedure having the same name."

    Here's the code, modified:

    Private Sub Form_Unload()
    Dim Msg, Style, TITLE, Help, Ctxt, Response, MyString
    Msg = "You have not entered all of the required fields. Do you want to exit?" ' Define message.
    Style = vbYesNo ' Define buttons.
    TITLE = "Incomplete record" ' Define title.
    Dim stDocName As String
    Dim stLinkCriteria As String

    If IsNull(Me.DEPT) = True Or IsNull(Me.ORDER_TYPE) = True Or IsNull(Me.TITLE) = True Or IsNull(Me.AUTHOR) = True Or (IsNull(Me.SERIES_NAME) = True And Me.SERIES = 0) Then
    Response = MsgBox(Msg, Style, TITLE)
    If Response = vbYes Then
    Me.Undo
    ElseIf Response = vbNo Then
    Cancel = True
    End If
    End If

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Closing out of a form - deleting incomplete record (2000)

    I didn't mean to just rename the event in the code. Unload accepts a Cancel argument, so it's proper declaration is like this:

    <code>Private Sub Form_Unload(Cancel As Integer)
    ... paste your code here
    End Sub</code>

    And I'm being picky, so take a look at your Dim statement.

    Dim Msg, Style, TITLE, Help, Ctxt, Response, MyString

    You're declaring all those variable as variants, which wastes resources and makes mistakes easier to overlook. Msg and TITLE are strings and Style is a Long. Ctxt and MyString aren't even used. Nor are stDocName or stLinkCriteria, which are declared a bit lower. Weeding out unused variables makes it a lot easier to read your code, and declaring specific datatypes helps you find coding errors more rapidly.
    Charlotte

  5. #5
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing out of a form - deleting incomplete record (2000)

    I actually copied that straight out of the MS Access help [img]/forums/images/smilies/smile.gif[/img] Thanks for the suggestions, I will definitely go back and clean it up.

    I changed the code as you suggested and it will cancel out of the msgbox and bring me back to the form, but the Me.Undo command isn't working. I click "Yes" that I would like to exit and then check the table to which the form is connected, it still has my incomplete record. Am I not using the correct command?

    Thanks for all of your help Charlotte -- i really appreciate it!

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Closing out of a form - deleting incomplete record (2000)

    What routine did you copy the code from? Me.Undo works in the Form_BeforeUpdate. After that, the record has already been saved, so you would have to delete it. The way you normally prevent this kind of situation is to structure the keys and required fields in your table so that the record can't be saved if certain information is missing.
    Charlotte

  7. #7
    2 Star Lounger
    Join Date
    Apr 2003
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing out of a form - deleting incomplete record (2000)

    I copied the code out of the MsgBox routine code that is in the MS Access help.

    Ahhhh.... Okay -- I added a delete record command instead of me.undo. That worked!

    Thanks so much for all of your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •