SOLVED

Running VBA code after clicking on a UserForm 'image'

Copper Contributor

Hello,

 

I am currently trying to write some VBA code that will initialize once I left click on a UserForm image I have uploaded. In my specific situation, I am using a hovering effect for a 'Login' button that will highlight a white 'Login' button/image green when hovered over with the mouse. This is done by hiding the white image and only showing the green image. See below, where I just have the white image over top the green one.

ryanbarton324_1-1619446327598.png

 

That part works fine right now, but the goal after this green highlight is to select the green button with the mouse, and after left clicking, some code will run. This is not working right now. I looked up on other forums and it looked like all I needed to do was have the code run with a '_Click()' sub in the code, but when I run a _Click for the image, an error returns saying below.

ryanbarton324_0-1619446248533.png

 

Whenever I right click on the image itself and do 'View Code', it creates a new Private Sub for '_BeforeDragOver()' and when I look in the top right drop down menu, there is no option for 'Click'. When I have the below code, the same error from above is returned. And when using the '_BeforeDragOver()' created Sub, nothing would happen when I do anything with the image.

 

ryanbarton324_2-1619446567571.png

 

 

Is there a new way to do this in the most recent Excel? Or is this not feasible anymore?

 

All other forums seemed to point to using a _Click sub for the image but I can't seem to make it work.

 

Any help is appreciated.

 

Thank you,

 

Ryan

 

14 Replies

@ryanbarton324 

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.

@Hans Vogelaar 

 

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)

ryanbarton324_0-1619456725499.png

 

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

 

@Hans Vogelaar 

 

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

@ryanbarton324 

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.

@Hans Vogelaar

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

@ryanbarton324 

Could you attach a sample workbook without sensitive information that demonstrates the problem?

@Hans Vogelaar 

 

Apologies for the delayed response, thanks for the reply as always.

 

I've gone ahead and created a simplified Excel file that has both of the UserForms, 'frmLogin' (One giving me issues) and then one 'frmMainMenu' that should pop up when the correct login information is given and 'Login' is selected in the login userform. I annotated all of the buttons within the code if that helps find the issue.

 

I removed the functionality of every other button from the main menu, so there should be minimal code to hopefully find a solution.

 

All I need is to somehow go from entering in the username: fhwa and password: 1234 in the frmLogin, then selecting the 'Login' green button/picture, then the login form will close itself and the main menu will pop up.

 

Thanks again for the help.

 

Ryan

best response confirmed by ryanbarton324 (Copper Contributor)
Solution

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.

@Hans Vogelaar

Oh my, how embarrassing.

I deleted that extra TextBox and now it works as intended. Not sure how that extra TextBox got there but that definitely explains the problems.

Thank you for all the help on this! Hopefully I won't make the same mistake in the future.

Best Regards,

Ryan
As a matter of fact, you can have a click event for an image control. It appears as a deprecated item in the Object Browser if you show hidden members. You can't add it using the usual dropdown in the code module header, but you can write a sub named "Image1_Click()" (or whatever the image's name is), and it will work as expected.

I don't know why it wasn't working for this user. I'd suggest it was a typo, but I'm the only bad typist in this forum :).
Sometimes that happens if I hold Ctrl to select multiple controls. If I move the cursor slightly while clicking on a control, I can end up with duplicate controls that line up perfectly with the originals but obscure them.

@Hans Vogelaar @ryanbarton324 

I am not sure of if this thread is actual, however, it looks like Image control has _Click event, despite it doesn't present in the VBA list.

If you type directly:

Private Sub Image1_click()

Excel will call that procedure on clicking the image area.

I have written an add-in for Excel 2003 for getting points from scientific graphic files.

It works well in Excel 2021.

Sincerely,

Andrey

As I stated, the Click event dows not appear in the events dropdown of the UserForm code module when an image is selected in the Objects dropdown, but if you open the Object Browser, scroll to Image in the list of classes, right click on the list of members, and choose Show Hidden Members, you will see a gray entry for Click. This means you can write your own _Click event procedure from scratch.
1 best response

Accepted Solutions
best response confirmed by ryanbarton324 (Copper Contributor)
Solution

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.

View solution in original post