Forum Discussion
Running VBA code after clicking on a UserForm 'image'
- Apr 28, 2021
There is a textbox named TextBox1 on top of the Password text box.
So you actually enter the password in TextBox1, and the Password text box remains empty. Hence the message that the login information is incorrect.
Added later: the Image control actually does have a Click event, but it is hidden. See the replies by JonPeltier below.
The Image control does not have a click event. you can use the MouseUp (or MouseDown) event instead, with syntax
Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' your code here
End Sub
with the name of your control instead of Image1, of course.
Thank you for the reply! Very much appreciated!
That did work for me to be able to click on the button after the highlight. Now, I have a different problem, hopefully minor.
So I have code that will run when the MouseUp sub is run after I left click on the 'Login' button, and it should be checking the login credentials and verifying that they are correct with a username and password. If the username and/or password is incorrect, it will return a message saying 'ATTENTION: Wrong Login Information'. If not, the main menu UserForm will pop up and the login form will be unloaded. See code below.
Private Sub OKButton_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Me.Username.Value = "fhwa" Then
If Me.Password.Value = "1234" Then
Unload Me
frmMainMenu.Show
Exit Sub
End If
End If
MsgBox "ATTENTION: Wrong Login Information"
End Sub
When testing, it works correctly when the wrong info is inputted, but it also returns the error message when I input the correct login credentials as well. See below (Password is 1234)
Do you know where in the code I might be going wrong? Are there some extra steps that need to be taken in order to have another UserForm open up based on user inputs?
Does the Me.Username.Value have to be directly referenced from the login form now that I'm using pictures instead of command buttons?
Thanks,
Ryan
- HansVogelaarApr 26, 2021MVP
Try this version:
Private Sub OKButton_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If Me.UserName.Value = "fhwa" And Me.Password.Value = "1234" Then Unload Me frmMainMenu.Show Else MsgBox "ATTENTION: Wrong Login Information" End If End Sub
It shouldn't make a difference whether you use an image or a command button.
If you need to refer to the username elsewhere, you could store the value of Me.UserName in a public variable that has been declared in a standard module. It will then be available to all code in the workbook.
- ryanbarton324Apr 26, 2021Copper ContributorHansVogelaar
Unfortunately, that other code returned the same problem.
The fact that I was able to get to the other menu through this method, barring the username/password input, leaves me optimistic, but still unsure on why this isn't working. Not a lot of extra code to be making problems. Although, I'm not too experienced with coding in UserForm Images.
I may try and find some other way to code out the username/password check, but seeing that your revision returned the same error, I don't really know where to start. That would've been my guess, doing an AND rather than a nested IF loop.
Any thoughts?
And thanks again for the help on this.
Ryan- HansVogelaarApr 26, 2021MVP
Could you attach a sample workbook without sensitive information that demonstrates the problem?
- ryanbarton324Apr 26, 2021Copper Contributor
I also will note that I tested out the below code, and it worked as intended. The main menu UserForm was opened up when the green Login button was selected. So it seems to me that its related to the Username/Password user inputs.
Private Sub OKButton_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) Unload Me frmMainMenu.Show End Sub
Ryan