Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 57
  1. #31
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727

    Quote Originally Posted by Micron View Post
    Close event was just an add-on trying to fix the problem, I think. I too thought it unnecessary because of the Unload event but didn't say so because of that.
    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

  2. #32
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Gicu View Post
    @data808: I think the Current event dirties the form and creates the empty record....
    The Unload event doesn't seem right, I would suggest you try the one I gave you in post #17 and see what happens. Also try the modified code for the login button.

    Cheers,
    Oh that is interesting. So this:

    Me.txtUsername = UCase(Me.txtUsername)

    may be the cause. I'm going to test that out right now.

  3. #33
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Gicu View Post
    @data808: I think the Current event dirties the form and creates the empty record....
    The Unload event doesn't seem right, I would suggest you try the one I gave you in post #17 and see what happens. Also try the modified code for the login button.

    Cheers,
    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.

  4. #34
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Welshgasman View Post
    Surely 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?
    Thanks. I added it to the Key Press event for the username text box itself and it seems to be working

    KeyAscii = Asc(UCase(Chr(KeyAscii)))

  5. #35
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Gicu View Post
    It should be in the AfterUpdate anyway, unless the username comes pre-populated from a local settings table holding the last user (which it should as the users should not have to type their username every time they log on).

    Cheers,
    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.

  6. #36
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Gicu View Post
    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,
    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.

    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.

  7. #37
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Gicu View Post
    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?

    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
    Cheers,

    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'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:

    Code:
    If IsNull(Me.txtUsername) And IsNull(Me.txtPassword) Then
        Me.Undo
        Exit Sub
    Else
        [Forms]![Login]![txtLogoutDateTime] = Now()
    End If
    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?

  8. #38
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by data808 View Post
    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:

    Code:
    If IsNull(Me.txtUsername) And IsNull(Me.txtPassword) Then
        Me.Undo
        Exit Sub
    Else
        [Forms]![Login]![txtLogoutDateTime] = Now()
    End If
    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?
    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:

    Code:
    If IsNull(Me.txtLoginDateTime) Then    
         Me.Undo
         Exit Sub
    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.

  9. #39
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I had Me.txtUsername.Value and you changed those to not have .Value. What is the difference?
    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 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.
    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:
    https://stackoverflow.com/questions/...ject-in-access
    Me.Dirty = False. Is that the same as saving the record because you are telling the form to not be Dirty anymore?
    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.
    What is the difference between making the form Me.Visible = False and Login form being hidden?
    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.

    Is the Quit button on the Login form? You can not
    specify that the Unload code only triggers if the Login form is hidden
    but you can easily add that condition to the code in the Unload event:
    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
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #40
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Found this online to prevent the user to close by taskbar:

    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
    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?

    Thanks @Gicu!

  11. #41
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    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,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  12. #42
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Gicu View Post
    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.

    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.

  13. #43
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    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.

  14. #44
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    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,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  15. #45
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by Gicu View Post
    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?

Page 3 of 4 FirstFirst 1234 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Code VBA for minimize application in Taskbar (Access)
    By ZakariaAadel in forum Programming
    Replies: 1
    Last Post: 11-14-2020, 07:38 AM
  2. Replies: 6
    Last Post: 12-08-2019, 04:28 AM
  3. Replies: 4
    Last Post: 01-11-2016, 08:50 AM
  4. Prevent Pinning of Access to Taskbar
    By Perceptus in forum Access
    Replies: 8
    Last Post: 07-07-2015, 11:33 AM
  5. Replies: 3
    Last Post: 03-17-2014, 10:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums