Application before_close event

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Is acccess application close event? like thisworkbook.close event? rather before close..
'm trying to make the event triger the code and maintain in and out from database log....



If anyone has anyidea...please help me out....

Thanks in advance
smile.gif
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I don't think Access does have a close event. But you can open a hidden form when the database opens. This form will have a close event that will fire if/when the database is closed.

You can use some code in your startup form for handling this (or the hidden form can simply *be* your startup form).
 
Upvote 0
Xen, thanks for the idea....
So how i do make this code...

do i enter something like
me.visible = false on load?


Thanks again...
 
Upvote 0
In the form Open event I have used this code:

DoCmd.Resize 0,0,0,0
 
Upvote 0
In the form Open event I have used this code:

DoCmd.Resize 0,0,0,0


Xen that perfect!!!

I tried this way and tried trigerring code on unload
the code runs and does not end....and then the access file stops responding....everytime i close the database..

Thanks again
Code:
[/FONT]
[FONT=Courier New]Private Sub Form_Unload(Cancel As Integer)[/FONT]
[FONT=Courier New]Call LogMeOut[/FONT]
[FONT=Courier New]End Sub
Code:
[/FONT]
[FONT=Courier New]Sub LogMeOut()[/FONT]
[FONT=Courier New]Dim db As DAO.Database
Dim rst As DAO.Recordset[/FONT]
[FONT=Courier New]Set db = CurrentDb
Set rst = db.OpenRecordset("BTLog", dbOpenDynaset)
'rst.MoveFirst
While Not rst.EOF
If rst!x_id = UCase(Environ("Username")) And rst!OutStatus = False Then
         rst.Edit
         rst!Logout = Now
         rst!Total = (rst!Logout - rst!Login)
         rst!OutStatus = True
         rst.Update
End If
rst.MoveNext
Wend[/FONT]
[FONT=Courier New]rst.Close
db.Close
Set rst = Nothing
Set db = Nothing[/FONT]
[FONT=Courier New]End sub[/FONT]
[FONT=Courier New]
 
Upvote 0
This works for me:

Two notes:
1) you are closing the db in your code which isn't necessary (the db is already being closed)
2) your total as "login - logout" is probably wrong as the logout field has not actually been updated until the record is updated. Use "now - login".

I don't know exactly what the problem is but I have re-written this with SQL commands instead of recordsets. Comment out the On Error statements until its tested - then I should add that back as there would be no reason for a user to deal with a crash should something go wrong at runtime.

Code:
Private Sub Form_Load()
Dim s As String
    
    DoCmd.MoveSize 0, 0, 0, 0
    
    On Error Resume Next
    s = s & " UPDATE [BTLog] SET [Login] = #" & Now & "#,"
    s = s & " [OutStatus] = False"
    s = s & " WHERE [x_id] = '" & Environ("UserName") & "';"
    
    CurrentDb.Execute s

End Sub
Private Sub Form_Close()
Dim s As String
    
    On Error Resume Next
    s = s & " UPDATE [BTLog] SET [Logout] = #" & Now & "#,"
    s = s & " [OutStatus] = True,"
    s = s & " Total = (Now - [Login])"
    s = s & " WHERE [x_id] = '" & Environ("UserName") & "';"

    CurrentDb.Execute s
    
End Sub
 
Upvote 0
Xen, thank you very much...:)
yea, i just realize that my total way was incorrect:biggrin:..
This is perfect!!!

I want to do this in different database if possible...:)


This works for me:

Two notes:
1) you are closing the db in your code which isn't necessary (the db is already being closed)
2) your total as "login - logout" is probably wrong as the logout field has not actually been updated until the record is updated. Use "now - login".

I don't know exactly what the problem is but I have re-written this with SQL commands instead of recordsets. Comment out the On Error statements until its tested - then I should add that back as there would be no reason for a user to deal with a crash should something go wrong at runtime.

Code:
Private Sub Form_Load()[/FONT]
[FONT=Courier New]Dim s As String[/FONT]
[FONT=Courier New] [/FONT]
[FONT=Courier New]    DoCmd.MoveSize 0, 0, 0, 0[/FONT]
[FONT=Courier New] [/FONT]
[FONT=Courier New]    On Error Resume Next[/FONT]
[FONT=Courier New]    s = s & " UPDATE [BTLog] SET [Login] = #" & Now & "#,"[/FONT]
[FONT=Courier New]    s = s & " [OutStatus] = False"[/FONT]
[FONT=Courier New]    s = s & " WHERE [x_id] = '" & Environ("UserName") & "';"[/FONT]
[FONT=Courier New] [/FONT]
[FONT=Courier New]    CurrentDb.Execute s[/FONT]
[FONT=Courier New] [/FONT]
[FONT=Courier New]End Sub[/FONT]
[FONT=Courier New]Private Sub Form_Close()[/FONT]
[FONT=Courier New]Dim s As String[/FONT]
[FONT=Courier New] [/FONT]
[FONT=Courier New]    On Error Resume Next[/FONT]
[FONT=Courier New]    s = s & " UPDATE [BTLog] SET [Logout] = #" & Now & "#,"[/FONT]
[FONT=Courier New]    s = s & " [OutStatus] = True,"[/FONT]
[FONT=Courier New]    s = s & " Total = (Now - [Login])"[/FONT]
[FONT=Courier New]    s = s & " WHERE [x_id] = '" & Environ("UserName") & "';"[/FONT]
[FONT=Courier New] [/FONT]
[FONT=Courier New]    CurrentDb.Execute s[/FONT]
[FONT=Courier New] [/FONT]
[FONT=Courier New]End Sub

 
Upvote 0
I want to do this in different database if possible...

The form code, of course, can only run in the database the form is in. Typically if another database is involved, this just means linking your database to a table in the other database. I would avoid more complicated setups for now, if at all possible. Usually keeping it simple is best. I personally wouldn't even bother with logging users in/out activity - it doesn't matter most of the time (unless they are lawyers and are charging by the minute!).

ξ
 
Upvote 0
yes Xen, you're right...maybe i'll try to keep things simple as possible....login/out is just add on...

bout linking the table i needs to research more on it and see how it reallys works....

Thanks again Xen!:)

The form code, of course, can only run in the database the form is in. Typically if another database is involved, this just means linking your database to a table in the other database. I would avoid more complicated setups for now, if at all possible. Usually keeping it simple is best. I personally wouldn't even bother with logging users in/out activity - it doesn't matter most of the time (unless they are lawyers and are charging by the minute!).

 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top