Dialog Box on Form Close

oxidus5886

New member
Local time
Today, 14:51
Joined
May 27, 2009
Messages
6
Hi,

This is my first post and I am really really new to coding in VB so youll have to bear with me.

I am trying to create a form in access which has a close button which closes the form. I have tried to design the form as data entry only, so all record navigation has been removed from the form and it is set to add new record on open. This is all fine.

I need to make it so that when the form is closed, the form cancels and deletes any record currently being edited in the current run through. The whole idea is that there will be a sequence of forms which open for the user to input data relating to the same item across multiple forms. Also the user must not be able to change anything already submitted.

I also need a dialog box to appear to warn the user about loss of progress with the data entry when the form is attempting to close.

Here's what I have so far on close:

Private Sub Form_Close()

If Me.Dirty Then GoTo DIALOG:

GoTo CLOSEFORMNOSAVE:

DIALOG:

MsgBox "Exiting the Add Car system now will delete any progress made. Are you sure you want to exit?", vbYesNo

If vbYes Then
GoTo CLOSEFORM:
ElseIf vbNo Then
GoTo CANCELCLOSE:
End If

CLOSEFORM:

DoCmd.RunCommand acCmdDeleteRecord
DoCmd.Close acForm, "Add Cars", acSaveNo
Exit Sub

CANCELCLOSE:

Exit Sub

CLOSEFORMNOSAVE:

End Sub

I know that this is probably full of bugs and errors. Essentially the form just closes and saves any data entered so far. Please could someone help me with this, I have been tearing my hair out. I am very limited with what I know, only what I've picked up from forums and help files. I would like to keep the commands and structure similar if I can, otherwise I will just get lost.
smile.gif
.

Any help would be greatly appreciated.

Thanks,

Adam
 
DO NOT USE GOTO !!!!

Example of how to do this
Code:
If something = 2 then
    Do something
else
    Do something else
end if
You can also nest if's if you like:
Code:
If something = 2 then
    do something
else
    if x=y then
       Do this
    else
       do that
    end if
end if

About this bit...
MsgBox "Exiting the Add Car system now will delete any progress made. Are you sure you want to exit?", vbYesNo
If vbYes Then

Select MsgBox and press "F1" read how to use this "properly" in the HELP.. It is there to ... help you :)
 
Thanks for the help namliam!

Ok, have looked up the help for that specific msgbox command, didnt even know you could get specific help by selecting and F1. :D.
I got rid of all of the Gotos.
I have this and it still doesn't work, could somebody steer me in the right direction again please.

Code:
Private Sub Form_Close()
 
Dim Msg, Style, Title, Response
 
If Me.Dirty = True Then
     Msg = "If you close the Add Car system now, you will lose any progress so far. Are you sure you want to continue?" 
     Style = vbYesNo + vbCritical + vbDefaultButton2 
     Title = "Data Warning" 
     Response = MsgBox(Msg, Style, Title, Help, Ctxt)
     If Response = vbYes Then 
          DoCmd.RunCommand acCmdDeleteRecord
          DoCmd.Close acForm, "Add Cars", acSaveNo 
     Else 
          Exit Sub
     End If 
End If
 
End Sub

Thanks for all your help.

Adam
 
Last edited:
The problem is that you are using it on the wrong event. You can't use it in the form's Close event because by that time the record has already been committed or abandoned. You need to use the form's BEFORE UPDATE event to validate your record and determine whether to move forward or cancel. You would check for fields not filled out and issue a Cancel = True if you want to cancel the update. Canceling the update will stop the form from closing. If you want to abandon the record then you can use a Me.Undo to clear the form and then close it.
 
Hi thanks for your input SOS.

The issue is what the end result needs to be, I will clarify.

The end result is to be a wizard type system using a series of forms one after the other. Each form allows the user to input more information but only for the one record the user has been working on.
e.g. Form 1 the user enters the registration plate of a vehicle
Form 2 the user enters the colour of the same vehicle and so on
I have removed all record navigation from this particular sequence and have set the On load event to start a new record rather than displaying an existing one.

The idea behind the code im trying to write is that the user is not able to quit out of the series of forms half way through and save any data entered so far.

So essentially what im trying to do is when the user attempts to exit the form, either by right clicking and close or by using the custom button on the form, access needs to check whether any new data has been added in the current series of forms.

If data has been added, dialog box and then if the user still wishes to close, it needs to remove the current record.

Does the Before Update event still apply?

An example code would be cool if anyone still has enough time and patience to deal with my noobish self. :D.

Thanks,

Adam.
 
Hmm, have to think about that one a bit.
 
Adam,

I have a couple of questions for you.

You indicate that you are using a series of forms to do the data entry and that this functions much like a Wizzard would function.

First questions: Is all of this data being entered into the same table or is the data in multiple tables with some sort of relationship between them?

Next question: Just how many forms are you using for your "Wizzard" type process?

A few years ago I had a couple of situations where there was a specific series of data enrties that needed to be made and they needed to be made in a specific order. In my case all of the data was being entered into the same table. What you are currently working on sounds very much like what I as trying to do. After trying several approaches, I finally decided to use a tab control in one bound form. this made it possible for me to only have to check the condition of the record in one form.

By using a tab control that had the tabs hidden, and using some Back and Next buttons on each of the tabs, I was able to make the one form actually work, look and function almost exactly like any other Wizzard.

Then when users clicked a Cancel button, all I had to do was have code like:

If Me.Dirty = true then
Me.Undo
endif
Docmd.Close acForm, "NameOfForm"

This is just one more way to handle the situation.

The problem that you will have with actually using multiple forms is that you will need to check to see if a record has created and if any data has already been written to the record and if so, you will need to delete that record.

HTH
 
Thanks for the help namliam!

Ok, have looked up the help for that specific msgbox command, didnt even know you could get specific help by selecting and F1. :D.
Great isnt it?

I got rid of all of the Gotos.
I have this and it still doesn't work, could somebody steer me in the right direction again please.
Much better, even if you are using the MS faulty DIM

Dim Msg, Style, Title, Response
This sets all variables as "Variants" which is basicaly a bad thing, without going into details ...
You always want to do
Dim Msg as string, Tittle as String etc...

Always add "As somethign" where womething obviously is the right type for your variable.


I would try different events to place your code and see if it works there. Possibly do a google or something, I am always messing the this type of thing as well...
 

Users who are viewing this thread

Back
Top Bottom