+ Reply to Thread
Results 1 to 21 of 21

Login form not reacting to code

Hybrid View

  1. #1
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Login form not reacting to code

    Hi

    I have a 3 userforms and 3 worksheets userform1 is the main form. what I want to happen is when workbook is opened user is presented with a login screen (userform3) they enter their username and password and then userform3 is hidden and userform1 opens .Within userform1 is a textbox (textbox10) i want the details entered to display in textbox10 and remain there as a running log of who logged in and when these details will also be recorded on sheet3 in columns A2(User) and B2 (Password) my code is below but does not work nothing goes into the textbox nor sheet 3. The code does not throw up any errors .Can anyone spot any errors ,
    Dim user As String
    Dim password As String
    Dim loginTime As String

    user = Me.txtuserid.Value
    password = Me.txtpassword.Value

    'check if username and password are correct
    If (user = "philp" And password = "et") Or (user = "Luml" And password = "et") Then

    'store current date and time
    loginTime = Format(Now(), "dd-MM-yyyy hh:mm:ss")

    'hide Userform3 if it's open
    If UserForm3.Visible = True Then
    UserForm3.Hide
    End If

    'show Userform1 and add login information to textbox
    UserForm1.Show
    UserForm1.TextBox10.Value = ""
    UserForm1.TextBox10.Value = user & " logged in at " & loginTime

    'append login information to a log file or worksheet
    Dim loginArray As Variant
    loginArray = Worksheets("sheet3").Range("A2:b2").Value
    For i = 1 To UBound(loginArray)
    UserForm1.TextBox10.Value = UserForm1.TextBox10.Value & loginArray(i, 1) & " logged in at " & loginArray(i, 2) & vbNewLine
    Next i
    Else
    'display error message
    MsgBox "Your username or password is incorrect", vbOKOnly + vbCritical, "Invalid credentials!"
    End If
    End Sub

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    Any chance we could see the file?

    BSB

  3. #3
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    Unfortunately it has confidential information on it .If it is not obvious what is wrong I will have to leave it but thank you for your reply

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    It's difficult to say for sure without seeing the code in its entirety, but I suspect the problem is the scope of your variables.
    If the lines that Dim the user, password and time variables is within the userform module then they can only be used within that userform.
    What you're trying to do it pass them from one form to another, and that would involve you setting them as "public variables".
    This should happen in a standard module and will allow you to pass them from one form to another.

    I'll mock something up to better explain what I mean and will attach it here shortly.

    BSB

  5. #5
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    Give the attached a try.

    It doesn't do everything your code is meant to do, but it will do the bit you're having the problem with.

    Hope it helps point you in the right direction.

    BSB
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    Thanks for your efforts on this but it did not work despite having all macros enable it would not run but having looked at your code i dont think it would have worked as i tried similar code and it would not show the new userform (userform1) because there was already a modal form open (userform3) so i had to use this :

    if userform3.visible=true
    then userform3.hide
    userform1.show

    i think i will just leave it i'm spending mine and guys like you valuable time on this but thanks

  7. #7
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    Why does the form need to be modal? You can close it and open it again rather than hiding it, storing any necesary values as public variables. Or even make that form modeless? (this last idea would very much depend on the tool you're building and I couldn't say either way for sure without seeing it)

    This is definitely a problem that has a solution, just need to find it. And no worries about spending my time, I'm here to help and only when I'm free so no waste of my time at all

    Not sure why my sample file wouldn't work for you as it works just fine here. MS have put the overly protective security back in place which means if you download a file from here you'll not be able to run macros in it. However, if you save it to your computer, close it then open it again, the macros should run just fine.

    BSB

  8. #8
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    hi again

    Thanks for your patience.What i have done is removed the names and a few other things from the workbook like names etc which should not affect anything you wish to try. I have left the code i have in the cmdlogin button in case all you have to do is tweak it . If not there is no problem in deleting it and entering it correctly to gain access to the workbook as i presume the login box will still appear the username is sl and password is enter textbox 10 if the blue colored box on form1 and the details i hope will also end up in sheet 3 in columns a2:b2 but the most important thing is to see is all the logins in textbox 10 that remain even after login off and on again you can see who logged in on what day at what time
    Attached Files Attached Files

  9. #9
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    Leave it with me and I'll see what I can do.

    In the meantime, should TextBox10 on UserForm1 show just the last login or all historical logins?
    If the latter then you'll be better off using a ListBox.

    BSB

  10. #10
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    The latter and all those details should be stored on sheet3 thanks

  11. #11
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    Newest at the top or oldest at the top?

    BSB

  12. #12
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    newest on top thanks

  13. #13
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,410

    Re: Login form not reacting to code

    Taking in the 'moving goalposts' since first read.
    The log in is recorded in the Userform3 module and the listbox is then populated in the UserForm1 initialisation event.
    Had a little play with loading textboxes via a loop (saves a lot of code but needs sheet order and textbox order to sync with each other).
    you can use similar in the image switching code - also with your combobox there is no need for a search loop as there is a direct link between the .listindex and sheet row.
    Attached Files Attached Files
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  14. #14
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    @torachan - Hope you're well. I disappear for a short while to cook and eat dinner and when I get back you've pipped me to the post!

    @Steve5914 - For what it's worth, my version is attached. I've tinkered around a bit with the code and some of it takes a slightly different approach to torachan's version, but I think does the same job.
    I swapped TextBox10 on UserForm1 out for a ListBox as an alternative approach to it. You'll see that historic login date/times look a tad odd, but the format your code would stamp them to Sheet3 will mean they show up in the expected format.

    I've also added a couple of little alternatives to a few sections of your code. Feel free to ignore them, obviously, but they may be of interest, so I've added them in but commented them out so they don't affect anything.

    BSB
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    Thank you both for your help but as badley has also amended some other code for me i will stay with it and with him but i tried your demo and its doing what i want but now i have put your code back into my original file its not working how are you referencing what was textbox 10 in your demo list box1 as i have now removed textbox 10 and put a list box there which as i already have a list box in named listbox 2

  16. #16
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    Cancel above i just used the listbox row source seems to be happy with that (i hope) thank you for all your time and help im crap at vba so may come back to you should i get stuck again

  17. #17
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    Seem to have it working as should thanks again for both of your help

  18. #18
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,908

    Re: Login form not reacting to code

    I told you this was a problem with a solution

    Very happy we could help, and of course feel free to come back if/when you need more assistance with it. Here to help!

    From one Eastbournian to another, have a good day.

    BSB

  19. #19
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    Hi did not think i would be back this quick but something has occured i presume based on what i have done. basically what you did works great but I notice that sometimes when i load my userform the login box does not contain the info but it is on the worksheet. I also have another list box with contact details on another sheet which does show the details. I have noticed that despite both listboxes having the sheet and row source in properties that the info will only display if the relevant sheet is active under the userform. In other words if i view sheet 3 and go back to userform all the login info is displayed but my contact details are not .if i view sheet 4 and then go back to userform the login details are gone but my contact details display. Should both listboxes display based on the row source info in properties.

  20. #20
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,410

    Re: Login form not reacting to code

    you are probably experiencing one of the weaknesses of rowsource/additem methods of populating lists/combos - this is a known but largely undocumented problem that Microsoft have never resolved because its behaviour is not consistent.
    as you have noticed its occurrence can be linked to whichever sheet is active despite the sheet id been stated in the referencing.
    best practice is to populate an array from a range or preferably from a 'structured table' and use the array to load your userform controls.

  21. #21
    Registered User
    Join Date
    11-01-2022
    Location
    Eastbourne
    MS-Off Ver
    365
    Posts
    18

    Re: Login form not reacting to code

    Thanks for the info vba a strange beast

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. User Form - Login by sheet (code modification)
    By nickytraps in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-30-2019, 02:39 PM
  2. Need help in login vba code to unhide specific sheets depends on login info
    By AlaaEddin95 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2019, 08:19 PM
  3. [SOLVED] LogIn Form
    By tfilipe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-21-2018, 07:46 AM
  4. Login Form
    By sn152 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-05-2014, 01:34 AM
  5. [SOLVED] Login sheet rather than login form?
    By mdovey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2013, 05:17 PM
  6. Login Form
    By phatus in forum Access Programming / VBA / Macros
    Replies: 5
    Last Post: 11-06-2010, 09:56 AM
  7. login form
    By missing in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-04-2006, 04:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1