Thanks @Micron. I got it working again. I mentioned what I did to make it go away. lolyou might have a corrupt code page. Try decompile and re-compiling.
http://www.fmsinc.com/microsoftacces.../decompile.asp
Yup, saw that. At the top of the forum page is a link "How to attach files". Generally it's best to post your file rather than pm it.
EDIT - actually, I don't think you can pm attachments here.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Found out that everything seems to work in the On Unload event even when the user closes the app from the taskbar except for Me.Undo. I tested MsgBoxes with other triggers to see what is happening and everything gets triggered except for the Me.Undo. That is why a record cannot be prevented from saving if the user closes the app by taskbar. So annoying.
Will report back if I find anymore details.
I thought I had mentioned that record would have been saved by then?, so nothing to undo?
Please use # icon on toolbar when posting code snippets.
Cross Posting: https://www.excelguru.ca/content.php?184
Debugging Access: https://www.youtube.com/results?sear...bug+access+vba
Thought you were going to post a db copy. Or am I mixing up my threads?
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Would you know when the record is actually saved because I felt like it's still Dirty. Basically I start typing the username which makes it Dirty then immediately after that, I close app by taskbar. So is the record being saved first because Access thinks it's like a power outage or something and does the save then closes?
No @Micron. You are correct. I was going to post my DB but emailed to @Gicu first to see if he could figure out any solutions. He hasn't gotten back to me yet so if he can't figure it out then I'll post it here. Thanks.
Right after either the BeforeInsert or BeforeUpdate event I would have thought?
Please use # icon on toolbar when posting code snippets.
Cross Posting: https://www.excelguru.ca/content.php?184
Debugging Access: https://www.youtube.com/results?sear...bug+access+vba
Can it do that even without any code in those events? Is that a safety feature built into Access to preserve data during outages?
Is there a way to write VBA to do a check if a button was clicked or not? Like I could code in the Before Update event to check if login button was clicked and if it wasn't then Me.Undo? Would that work?
And if the login button was clicked then I don't want the "recordsave" to undo because that means they are actually trying to log in and I would want the record to save that data.
One way to cause a record to be saved is to move off of it, and that can include something as benign clicking some other control on a form. So there's probably not much point in worrying about whether or not a button was clicked. We are 4 pages of posts into this issue so in absence of a db to troubleshoot I'm just going to wish you luck and disengage.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Yeah I knew records do save when you move from one record to another thats why in other forms where I have the users enter records, I have code to take care of that stuff in the Before Update events. However, I also just tested another form that has this code I am referring to and it's resides in the Before Update event with a Docmd.RunCommand acCmdUndo and that did not trigger when closing the app by taskbar. Not sure if Docmd Undo makes a difference from Me.Undo but in all cases, it seems that Access has something built in as a safety to preserve data when there are electrical outages and I think closing from taskbar is something that replicates that outage scenario. Too bad Access doesn't implement something to counter this and be able to tell the difference when a user closes by taskbar or when there is an electrical outage.One way to cause a record to be saved is to move off of it, and that can include something as benign clicking some other control on a form. So there's probably not much point in worrying about whether or not a button was clicked. We are 4 pages of posts into this issue so in absence of a db to troubleshoot I'm just going to wish you luck and disengage.
Anyway, no worries. I totally understand your disengagement. If @Gicu can't figure out something I will post the DB. Just want to say thank you so much to everyone who has tried to help. You guys are awesome!
Thanks everyone for the help. @Gicu was able to help me come up with something for the Before Update event which gave the result for what I was looking for. Just thought I was post the solution in case it can help someone else out there with similar issues. Here is the code that was needed to achieve what I wanted. It all started with figuring out why the form was always Dirty even if the user didn't type anything into the form. As @Gicu pointed out, we removed this from the Form_Current event:
Me.txtUsername = UCase(Me.txtUsername)
Instead it was moved over to the Key Press "KeyAscii = Asc(UCase(Chr(KeyAscii)))" and After Update "Me.txtUsername = UCase(Me.txtUsername)" events and then the Dirty issue went away.
Then to resolve records from being saved with missing or no data when the user closed app by taskbar, I added this VBA to the following events:
Code:Private Sub Form_BeforeUpdate(Cancel As Integer) If IsNull(Me.txtLoginDateTime) And Me.Visible = True Then Me.Undo Exit Sub End If End SubCode:Private Sub Form_Unload(Cancel As Integer) If Me.Visible = False Then [Forms]![Login]![txtLogoutDateTime] = Now() Exit Sub End If End SubThe code for the login remained very close to the original code except for the order of when the form became hidden and when the record would be saved. To work with the Before Update event, I had to make sure the form would be hidden before it tries to save the record so that if the user closed the app by taskbar while still filling out or at the very least has the login form open and hasn't logged in yet, the Me.Undo would trigger so that the login date/time stamp would not occur. The Unload event will only trigger if the login form is hidden while it closes because if the form is hidden, that means the user had already logged in.Code:Private Sub cmdLogin_Click() Dim validCredentials As Integer Dim userLevel As Variant Dim ID As Integer validCredentials = DCount("Username", "[tblUser]", "[Username] ='" & txtUsername & _ "' AND [Password]='" & txtPassword & "'") If IsNull(Me.txtUsername) Then MsgBox "Please Enter Username", vbInformation, "Username Required" Me.txtUsername.SetFocus ElseIf IsNull(Me.txtPassword) Then MsgBox "Please Enter Password", vbInformation, "Password Required" Me.txtPassword.SetFocus Else ID = DLookup("UserID", "tblUser", "Username = '" & Me.txtUsername.Value & "'") If validCredentials = 1 And Me.txtPassword = "password" Then Me.Undo DoCmd.Close MsgBox "Please Change Password", vbInformation, "New Password Required!" DoCmd.OpenForm "Change Password", , , "[UserID] = " & ID Else If validCredentials = 1 Then userLevel = Nz(DLookup("UserSecurity", "[tblUser]", "[Username]='" & Me.txtUsername & "'"), "") If userLevel = "Admin" Then Me.Visible = False Me.txtLoginDateTime = Now() Me.Dirty = False 'Me.Visible = False DoCmd.OpenForm "Main Menu_admin" Else Me.Visible = False Me.txtLoginDateTime = Now() Me.Dirty = False DoCmd.OpenForm "Splash Screen Load" End If Else MsgBox "Invalid Username Or Password!", vbExclamation, "UNAUTHORIZED!" Me.txtPassword.SetFocus End If End If End If Exit Sub End Sub
Thanks again to everyone who helped with this issue. Appreciate it.