Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancelling record on form close. 1

Status
Not open for further replies.

Eldaria

Programmer
Sep 20, 2001
123
NL
Hi people,

I have a problem with users sometimes leaves the computer without shutting down the database, and then I have to run around and find out who is logged in and then shut it down manually to be able to save changes to the database.

Now I have made a function where I can send a message to users, and also kick them out of the database, simply by having the main menu running a timer, and then check a table every 5 seconds... and if a sertain value is set it will launch, docmd.quit acSaveAll, now that works until they are working on a record and leaves the database in the middle of entering data.

Is there anyway I can set the forms to cancel the record when it closes?
I tryed to see if the event "unload" could do it, but that one is executed after the "update" event.
I then thought of just setting all the fields on the form to Null, but that was a bad Idea, because if the person has an exeisting record open, that record will be cleared...
so it has to be some kind of function that cancells the form, like if the user pushed the Escape key locally.

Any input is appreciated...
Eldaria

That was my 25cent** of opinion.

** Inclusive Intrest, tax on interest, Genral tax, Enviromental tax, Tax, and tax on intrest, tax on fees, tax on tax, and other Various taxes and fees.
 
Eldaria,

Here is what I recommend. When your main form timer event determines that it is time to shut down the database, have it set a global variable BEFORE issuing the docmd.quit command.

Then, on each form, in the BeforeUpdate event, check the value of that global variable. If the value indicates that the database is shutting down, issue a me.undo command to undo changes on the form. Otherwise, do nothing (or do whatever you might already have in the before update event.

That way, during normal operation, the value of your global variable will be false and not affect the saving of records on forms. But, during your shutdown, forms will be told to undo their changes before saving the record.
 
Brilliant, Jist what I was looking for, I did not realise that there where a command called Undo. Oh well We learn something everyday...

Thanks a lot...
Eldaria

That was my 25cent** of opinion.

** Inclusive Intrest, tax on interest, Genral tax, Enviromental tax, Tax, and tax on intrest, tax on fees, tax on tax, and other Various taxes and fees.
 
Hmm, I tried it in action, but it did not work. It seems like the docmd.quit clears all variables before issuing the close command to the forms.
Eldaria

That was my 25cent** of opinion.

** Inclusive Intrest, tax on interest, Genral tax, Enviromental tax, Tax, and tax on intrest, tax on fees, tax on tax, and other Various taxes and fees.
 
Ok,

I hadn't actually tested my idea, but I was sure it would work. I am surprised to hear that issueing docmd.quit caused the value of your global variable to become invalid.

So, I have 2 solutions. The first possible solution is to work some backward logic on the situation. In other words, change your global variable to something like "NotShuttingDown" and set it's value to True right from the start. Then, your forms would look for it to become false before doing the .undo trick.

However, this may still backfire under certain error conditions. I have heard other reports from people that certain runtime errors cause global variables to be reset as well.

So, option 2 is probably the best, although it requires a little bit more coding. On each and EVERY form in your database, create a public subroutine that can be called as a method of the form. The subroutine name has to be exactly the same on every form. Which reduces the work because you can write it once and then copy/paste it into all of your forms. This subroutine (example below) will do the .undo work and issue a docmd.close for the form.

Your timer code that decides it is time to shutdown the database will now issue this command to EVERY open form before issuing the docmd.quit

Sample addition to timer event:

Dim frm as Access.Form
Dim x as Integer
....
(if it is time to shutdown the database)
For x = 1 to Forms.Count
Forms(0).RemoteShutdown
Docmd.Close acform, forms(0).name
next x

Note: You will notice that I deliberately reference forms(0) instead of forms(x). This is very important. Collections automatically renumber themselves when items are added or deleted from them. Closing a form causes it to be deleted from the collection. Therefore, referencing forms(x) would cause you to issue the command to only 1/2 of the open forms.

Now, a sample RemoteShutdown method:

Public Sub RemoteShutdown()
Me.Undo
(Any other cleanup that might be needed)
End Sub

This subroutine is very simple actually since all it needs to do is undo the changes on the form. Be sure to remove the code that I recommended previously from the BeforeUpdate event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top