Userforms - multiple image buttons - how to add code when clicking on one of the images?

Janb

New Member
Joined
Feb 19, 2009
Messages
39
Hi,

I have 15 images in a userform. The (Name) is Image1, Image2, Image3.....Image15.

Every image belongs to a textbox. The boxes are called: txtControleDatum1, txtControleDatum2, txtControleDatum3.......txtControleDatum15

When I click on 1 of the images a calender pops up. The goal is that when I click on image3, the date must be filled in txtControleDatum3.

The calender is on one form and the images and textboxes are on another form. I called them frmKalender and frmControlesToezicht.

I was thinking of the following solution which unfortunately doesn't work.

In my form frmControlesToezicht I add the following code (I declared KalenderButton as integer) - I show only the first 4 but it goes up to 15.

VBA Code:
Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    frmKalender.Show
    KalenderButton = 1
End Sub
Private Sub Image2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    frmKalender.Show
    KalenderButton = 2
End Sub
Private Sub Image3_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    frmKalender.Show
    KalenderButton = 3
End Sub
Private Sub Image4_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    frmKalender.Show
    KalenderButton = 4
End Sub

on the Kalender form (frmKalender) I am using this code to test if the output is correct.


VBA Code:
Private Sub btnBevestigDatum_Click()
 
    MsgBox "Kalenderbutton is: " & frmControlesToezicht.KalenderButton

    Unload Me

When I'm clicking on image 1 the result is: Kalenderbutton is: 0
When I'm clicking for the second time on image 1, the result is: Kalenderbutton is: 1
And after that when i'm clicking again and again on image1, the result is: Kalenderbutton is: 1

Next step is clicking on image2
The first time the result is: Kalenderbutton is: 1
The 2nd, 3rd, 4th.... time the result is: Kalenderbutton is: 2

and the same pattern goes on with image 3, 4 .....

The pattern I discovered is that when I'm clicking for the first time on an image, it starts with 0. Clicking for the 2nd, 3rd time etc. gives the correct result.
When I'm clicking on another image it starts the first time with the same number as the one I clicked before. And not only in the order 1, 2 , 3.

If I click the first time on image 3 it starts with 0 followed by 3,
If I click on a second button - for example button 1 - it starts with 3 followed by 1.

What am I doing wrong? What do I not see? Or what is a better approach?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi,
see if this helps

in your userform (frmKalender) ensure that your variable KalenderButton is declared Public at the VERY TOP of the userforms code page OUTSIDE any procedure

VBA Code:
Public KalenderButton As Long

Update form events in frmControlesToezicht as follows

VBA Code:
Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    With frmKalender
        .KalenderButton = 1
        .Show
    End With
End Sub
Private Sub Image2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    With frmKalender
        .KalenderButton = 2
        .Show
    End With
End Sub

etc etc

Kalender form button test

VBA Code:
Private Sub btnBevestigDatum_Click()
 
    MsgBox "Kalenderbutton is: " & Me.KalenderButton

    Unload Me
    
End Sub

Hope helpful

Dave
 
Upvote 0
Solution
Hé dmt323,

Thanks a lot!! Your suggestion didn't directly solve my problem. I understand that it's hard to come with the exact solution if you don't have the whole picture.

But your solution gave me enough insights to solve my problem. So in fact you "solved" my problem. Many many thanks again!!

The code that finally works:

VBA Code:
Private Sub Image2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Me.KalenderButton = 2
    frmKalender.Show
End Sub

The error:

Compile Error.png
 
Upvote 0
Hi,
see if this helps

in your userform (frmKalender) ensure that your variable KalenderButton is declared Public at the VERY TOP of the userforms code page OUTSIDE any procedure

VBA Code:
Public KalenderButton As Long

Update form events in frmControlesToezicht as follows

VBA Code:
Private Sub Image1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    With frmKalender
        .KalenderButton = 1
        .Show
    End With
End Sub
Private Sub Image2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    With frmKalender
        .KalenderButton = 2
        .Show
    End With
End Sub

etc etc

Kalender form button test

VBA Code:
Private Sub btnBevestigDatum_Click()
 
    MsgBox "Kalenderbutton is: " & Me.KalenderButton

    Unload Me
   
End Sub

Hope helpful

Dave
Thank yo so much Dave! See my extensive anser below this post.
 
Upvote 0
Hi,
You are getting that error because the Public variable I mentioned at top of my post needs to be placed as I directed - however, if found a solution then glad you have managed to resolve the issue

Appreciate your feedback

Dave
 
Upvote 0
Hi,
You are getting that error because the Public variable I mentioned at top of my post needs to be placed as I directed - however, if found a solution then glad you have managed to resolve the issue

Appreciate your feedback

Dave
Hi Dave,

Thank you for your feedback. I will study that again. I have a publci variable in my form but it was the wrong form. Thanks for this additonal comment.

Jan
 
Upvote 0

Forum statistics

Threads
1,213,485
Messages
6,113,931
Members
448,533
Latest member
thietbibeboiwasaco

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top