Yes exactly. It's not needed. I just added it to test if the Close event would work instead of the Unload event and they both didn't. lol
Oh that is interesting. So this:
Me.txtUsername = UCase(Me.txtUsername)
may be the cause. I'm going to test that out right now.
Omg. It worked!!
I just tested it and took out that Current event line for uppercase in the username text box and it doesn't assign an autonumber to the record if I don't type anything. It also no longer saves a blank record.
You are a genius @Gicu.
I also noticed that even after removing that Me.txtUsername = UCase(Me.txtUsername) the text that I type into the username text box is still uppercase so that line was not even needed for that. I set this all up a while ago and can't remember where else I may have created an upper case trigger for the username text box.
Nevermind. Found it. It's in the txtusername key press event:
KeyAscii = Asc(UCase(Chr(KeyAscii)))
So I guess that is safe to have to not make the form Dirty.
Thanks. I added it to the Key Press event for the username text box itself and it seems to be workingSurely the same applies as I mentioned for the Close event?
https://support.microsoft.com/en-us/...n-us&ad=us#bm3
Easy enough to check. Comment out the OnCurrent uppercase line and put it in the AfterUpdate of the control?
KeyAscii = Asc(UCase(Chr(KeyAscii)))
That sounds pretty awesome but the users change desks so frequently because of their job duty rotation that this auto populate of the last user in the username text box wouldn't be beneficial in our office environment. Thanks though as that is pretty cool that you can do that.
Thanks @Gicu for all the code changes and fixes. I will definitely give it a try once I get home. Would like to see if it clears up any other issues.Can you please create a small sample with just the login table, login form and switchboard and include any necessary objects (such as AutoExec macro) to make this work so we can see\test the scenarios. I have not yet see the entire code for the login form, how is it started (autoexec macro vs. startup form properties), etc.
The code you had originally had some issues that I tried to change\correct (for example you were opening the form again hidden using OpenForm method instead of simply setting the Visible property to false for the current instance). Have you tried any of the code changes I suggested?
Cheers,
Not sure what a switchboard or AutoExec macros are. If anything, the login form is probably started by form properties but not sure. If all else fails then I'll try working on a test login db for you to try out.
I'm in the middle of testing your code. Seems to be working but I am not finished yet. I do have some questions on the changes you made so I can try to understand what is happening. What are the differences between these lines:I modified a bit your code, could you give it a try? Regarding the empty records, do you start the form in data add mode? Any code in the Load or Open events?
Cheers,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 & "'") ID = DLookup("UserID", "tblUser", "Username = '" & Me.txtUsername & "'") If validCredentials = 1 Then userLevel = Nz(DLookup("UserSecurity", "[tblUser]", "[Username]='" & Me.txtUsername & "'"), "") 'was 0 on the next line you look for a string If userLevel = "Admin" Then Me.txtLoginDateTime = Now() Me.Dirty = False 'DoCmd.RunCommand acCmdSaveRecord Me.Visible=False 'DoCmd.OpenForm "Login", acNormal, , , acFormEdit, acHidden DoCmd.OpenForm "Main Menu_admin" Else Me.txtLoginDateTime = Now() Me.Dirty = False 'DoCmd.RunCommand acCmdSaveRecord DoCmd.OpenForm "Splash Screen Load" End If Else MsgBox "Invalid Username Or Password!", vbExclamation, "UNAUTHORIZED!" Me.txtPassword.SetFocus End If End Sub Private Sub Form_Unload(Cancel As Integer) 'If Me.Dirty Then 'Me.Undo 'Exit Sub 'End If If IsNull(Me.txtUsername) And IsNull(Me.txtPassword) Then Me.Undo Exit Sub Else [Forms]![Login]![txtLogoutDateTime] = Now() End If End Sub
You might want to review my free sample http app that has the auto-logon feature suggested by Micron and also a nice custom chat feature you could use to ask your users to get out for maintenance://forestbyte.com/ms-access-utilities/fba-ms-access-custom-chat/
You can also force them out adding a table to the back-end with a Yes\No field which you would check when you want them out; I usually implement this in conjunction with the "shut down on idle" code I sent you earlier. Let me know if you're interested to see it and I'll dig it out.
Cheers,
I had Me.txtUsername.Value and you changed those to not have .Value. What is the difference?
For this line you did the first one and I did the second one:
userLevel = Nz(DLookup("UserSecurity", "[tblUser]", "[Username]='" & Me.txtUsername & "'"), "")
userLevel = Nz(DLookup("UserSecurity", "[tblUser]", "[Username]='" & txtUsername & "'"), 0)
I noticed you added the Me.? So does this mean that you are now pulling from the form itself instead of the Dlookup? You explained removing the 0 in the notes.
Me.Dirty = False. Is that the same as saving the record because you are telling the form to not be Dirty anymore? What is the difference between making the form Me.Visible = False and Login form being hidden? Does it matter?
I did find one conflict so far in the Unload event and the Quit button. I had both credentials typed out (username and password) and then decided to just click the Quit button. I believe the Quit has the Me.Undo line as well. However, because the Unload event has this:
This is what probably happened. It saved a record with no username or login date/time because the Quit Me.Undo took care of that but because of the Unload event code, it saved the logout date/time because both fields were filled out. Maybe we have to specify that the Unload code only triggers if the Login form is hidden? If it's not hidden then we don't want the logout date/time to happen. Is that possible?Code:If IsNull(Me.txtUsername) And IsNull(Me.txtPassword) Then Me.Undo Exit Sub Else [Forms]![Login]![txtLogoutDateTime] = Now() End If
Ok I think I fixed that issue I mentioned with the record saving if I have both credentials filled out and click the Quit button. I added this line to the Unload Event at the top:I'm in the middle of testing your code. Seems to be working but I am not finished yet. I do have some questions on the changes you made so I can try to understand what is happening. What are the differences between these lines:
I had Me.txtUsername.Value and you changed those to not have .Value. What is the difference?
For this line you did the first one and I did the second one:
userLevel = Nz(DLookup("UserSecurity", "[tblUser]", "[Username]='" & Me.txtUsername & "'"), "")
userLevel = Nz(DLookup("UserSecurity", "[tblUser]", "[Username]='" & txtUsername & "'"), 0)
I noticed you added the Me.? So does this mean that you are now pulling from the form itself instead of the Dlookup? You explained removing the 0 in the notes.
Me.Dirty = False. Is that the same as saving the record because you are telling the form to not be Dirty anymore? What is the difference between making the form Me.Visible = False and Login form being hidden? Does it matter?
I did find one conflict so far in the Unload event and the Quit button. I had both credentials typed out (username and password) and then decided to just click the Quit button. I believe the Quit has the Me.Undo line as well. However, because the Unload event has this:
This is what probably happened. It saved a record with no username or login date/time because the Quit Me.Undo took care of that but because of the Unload event code, it saved the logout date/time because both fields were filled out. Maybe we have to specify that the Unload code only triggers if the Login form is hidden? If it's not hidden then we don't want the logout date/time to happen. Is that possible?Code:If IsNull(Me.txtUsername) And IsNull(Me.txtPassword) Then Me.Undo Exit Sub Else [Forms]![Login]![txtLogoutDateTime] = Now() End If
Probably need to test it for more scenarios but for the issue that I mentioned in the previous post, it took care of it.Code:If IsNull(Me.txtLoginDateTime) Then Me.Undo Exit Sub End If
Value is the default property of the textbox control so it is not needed. I tend to rely on those default properties to make the code shorter. For example .Fields is the default collection for recordsets so rs.Fields("YourField") can be shortened to rs("YourField").I had Me.txtUsername.Value and you changed those to not have .Value. What is the difference?
Using Me. makes the code more readable, gives you the benefit of Intellisense to avoid spelling mistakes in control names and sometimes helps you avoid ambiguities, have a read below:I noticed you added the Me.? So does this mean that you are now pulling from the form itself instead of the Dlookup? You explained removing the 0 in the notes.
https://stackoverflow.com/questions/...ject-in-access
Basically yes, but the advantage is that you know exactly which record you are saving; sometimes the code inadvertently sets focus on another form or a subform and calling the DoCmd.RunCommand acCmdSaveRecord will save the wrong record.Me.Dirty = False. Is that the same as saving the record because you are telling the form to not be Dirty anymore?
You are already in the Login form so to hide it you set its visible property to false; if you run the Docmd.OpenForm it will not create a new instance of the form, but it might trigger some events a second time (Current comes to mind, I know the Open one will not trigger), so it might impact your envisioned flow.What is the difference between making the form Me.Visible = False and Login form being hidden?
Is the Quit button on the Login form? You can notbut you can easily add that condition to the code in the Unload event:specify that the Unload code only triggers if the Login form is hidden
Cheers,Code:If Me.Visible =False 'form is hidden 'do stuff you want to happen when the form is hidden Else 'do stuff you want to happen when the form is visible End If
Found this online to prevent the user to close by taskbar:
Do you think this would work if I did this in the On Unload event for the login form? If so, would you be able to help modify it to get it to work?Code:Public ExitButtonPressed As Boolean Public Sub Form_Unload(Cancel As Integer) If ExitButtonPressed = True Then Exit Sub Else Cancel = True MsgBox "Hey fellow, please click the button to close the app!" End If End Sub Public Sub cmdsluiten_Click() 'Function to close application On Error GoTo Err_cmdSluiten_Click ExitButtonPressed = True Application.Quit acPrompt Exit_Me: Exit Sub
Thanks @Gicu!
Not sure you would need this. And if you look at what it does you'll see that it might create some issues if the form is hidden as you cancel the unload but you don't provide the user a way out unless you make the Login visible again so they can click your Quit button to set the public variable (ExitButtonPressed) to True. Have you tried my suggestion to modify the Unload event based on the value of the Me.Visible property? I think in there you could implement the approach above if the login form is visible, but shouldn't the end result be the same regardless of how the user closes the database?
Please show the entire code module for the Login form as you have it now so we can see your latest changes.
Cheers,
Yeah, later I'll try and see if I can test out that "if form is visible" code and see if I can resolve some of the issues.Not sure you would need this. And if you look at what it does you'll see that it might create some issues if the form is hidden as you cancel the unload but you don't provide the user a way out unless you make the Login visible again so they can click your Quit button to set the public variable (ExitButtonPressed) to True. Have you tried my suggestion to modify the Unload event based on the value of the Me.Visible property? I think in there you could implement the approach above if the login form is visible, but shouldn't the end result be the same regardless of how the user closes the database?
Please show the entire code module for the Login form as you have it now so we can see your latest changes.
Cheers,
Right now I am in the middle of trying to prepare a sample of the database for you to look at. I have tried to remove all sensitive and unnecessary data. I think I got it all but when I compact and repair I get an error message:
The expression On Open you entered as the event property setting produced the following error: Return without GoSub.
* The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
* There may have been an error evaluating the function, event, or macro.
you might have a corrupt code page. Try decompile and re-compiling.
http://www.fmsinc.com/microsoftacces.../decompile.asp
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
If you're sure you removed all sensitive data don't worry too much about that error, just zip it and upload it and I'll have a look.
Cheers,
I got it working again. The error went away when I reopened the welcome splash screen in design view and looked at the VBA and saved it. Odd thing is I didn't touch the VBA, just viewed it and saved the form then the error went away.
Anyway, just to make sure, I cleared out the backend tables but left a couple user credentials for examples for you to test out. Is there any other areas that would have sensitive information? Should I send this to you through DM or something?