G
Guest
I appear to be losing a recordset object when closing the application with a
DoCmd.Quit. However when I close the form itself or use the close box of
Access, I do not lose it and everything works as I expected.
I am attempting to track logins and keep a persistent connection to my
backend database to avoid any lock file performance hits. If I can get this
working I have solved several problems (possilbe performance increase,
identify users doing ctl+alt+del for exit, auto exit if application is left
idle, report application activity, etc.).
I have reduced the code to two forms to show the behavior. The table can be
in the same database or linked. When you use the exit button on the Launch
Edit window, you will get the message that the recordset is lost. Close the
Dummy window or use the close box of the Access application and it reports it
is working. Why am I losing the recordset object and what can I do to get
around this?
The code for form frmLaunchEdit (unbound form with single command button) is:
Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
DoCmd.OpenForm "frmDummy"
End Sub
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
DoCmd.Quit
Exit_Command0_Click:
Exit Sub
Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click
End Sub
The code for form frmDummy (unbound form with no controls) is:
Option Compare Database
Option Explicit
Public LoginRS As Recordset
Private Sub Form_Open(Cancel As Integer)
Dim timeStamp As Date
' Capture a timestamp (date and time)
timeStamp = Date + Time
' Open a connection and add a login record for the user.
Set LoginRS = CurrentDb.OpenRecordset("tblLogins")
With LoginRS
.AddNew
!UserId = "dummy"
!userName = "test app"
!Login = timeStamp
!LastActivity = timeStamp
.Update
' Set the cursor on the added record.
.Bookmark = .LastModified
End With
End Sub
Private Sub Form_Close()
If LoginRS Is Nothing Then
MsgBox "Recrodset is gone"
Else
MsgBox "Closing..."
With LoginRS
.Edit
!logoutby = "logout trapped!"
.Update
.Close
End With
End If
End Sub
The table tblLogins is defined as:
UserId - text, indexed
UserName – text
Login – data/time, indexed
LastActivityDate – date/time
Logout – date/time
LogoutBy – text
None of the columns are required.
DoCmd.Quit. However when I close the form itself or use the close box of
Access, I do not lose it and everything works as I expected.
I am attempting to track logins and keep a persistent connection to my
backend database to avoid any lock file performance hits. If I can get this
working I have solved several problems (possilbe performance increase,
identify users doing ctl+alt+del for exit, auto exit if application is left
idle, report application activity, etc.).
I have reduced the code to two forms to show the behavior. The table can be
in the same database or linked. When you use the exit button on the Launch
Edit window, you will get the message that the recordset is lost. Close the
Dummy window or use the close box of the Access application and it reports it
is working. Why am I losing the recordset object and what can I do to get
around this?
The code for form frmLaunchEdit (unbound form with single command button) is:
Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
DoCmd.OpenForm "frmDummy"
End Sub
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
DoCmd.Quit
Exit_Command0_Click:
Exit Sub
Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click
End Sub
The code for form frmDummy (unbound form with no controls) is:
Option Compare Database
Option Explicit
Public LoginRS As Recordset
Private Sub Form_Open(Cancel As Integer)
Dim timeStamp As Date
' Capture a timestamp (date and time)
timeStamp = Date + Time
' Open a connection and add a login record for the user.
Set LoginRS = CurrentDb.OpenRecordset("tblLogins")
With LoginRS
.AddNew
!UserId = "dummy"
!userName = "test app"
!Login = timeStamp
!LastActivity = timeStamp
.Update
' Set the cursor on the added record.
.Bookmark = .LastModified
End With
End Sub
Private Sub Form_Close()
If LoginRS Is Nothing Then
MsgBox "Recrodset is gone"
Else
MsgBox "Closing..."
With LoginRS
.Edit
!logoutby = "logout trapped!"
.Update
.Close
End With
End If
End Sub
The table tblLogins is defined as:
UserId - text, indexed
UserName – text
Login – data/time, indexed
LastActivityDate – date/time
Logout – date/time
LogoutBy – text
None of the columns are required.