Using imgaes to perform action instead of command button

aman

Registered User.
Local time
Today, 12:34
Joined
Oct 16, 2008
Messages
1,251

My manager has given me the icons that can be converted to images and used in Access forms . So he wants me use click events on the images to perform an action. He doesn't like command buttons as they don't look very appealing so he wants me to use these icons. SO basically I select an icon in the paint brush , copy and paste in Access form and then right click on it to change it to Image.

And then I write click event on it to perform actions.

I have used them and they work fine . But the only thing is when the user click on it there should be a change in the background on the image or something else to make clear that image has been clicked. Because sometimes the user press it twice or thrice as they don't know whether it has been clicked or not.

Thanks
 
Leave it to a manager to re-invent the wheel and make MORE work out of nothing.

you can put a second, invisible image that is darker/faded version of the visible one.
when image1 is clicked, make image1dark.visible = true
until the process ends.
 
Best way to do this would be to add a line at the beginning of the "On Click" event. Something like:

Me.TextBoxName.BackColor = 12632256 '(gray)

...rest of your code...

Let me know if it works!
 
You are aware you can put images on buttons?

There are some options:

1. have 2 images, one for the unclicked state and one for the clicked state. Then in your click event you need some code to swap the images.

2. the other possibility is to provide transparency to create transparent images - if so you would use code to change the background colour.

By transparent images, I mean the background of the image - if you use photoshop or similar you can define the background as being a particular colour and then set it to transparent. However Access does not always render these particularly well

3. easiest is to make use of the border properties - perhaps transparent when unclicked and red when clicked - or change the thickness, or type from raised to sunken for example
 
Or you can just use the MouseUP & MouseDown events where you include the height & width of the images.

Here is a sample:
Code:
Private Sub yourImageName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.yourImageName.Height = 500
Me.yourImageName.Width = 500
End Sub
----------------------------------------------------
Private Sub icoLogout_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.yourImageName.Height = 450
Me.yourImageName.Width = 450
End Sub
 
Thanks CJ_London. This is exactly what I was looking for. many Thanks :)
 
Many Thanks Richardw. Never thought this could be done this way. That worked perfectly fine. :)
 

I have used them and they work fine . But the only thing is when the user click on it there should be a change in the background on the image or something else to make clear that image has been clicked. Because sometimes the user press it twice or thrice as they don't know whether it has been clicked or not.

Thanks
Also consider a message box as user feedback informing them what they have done or are about to do. A simple "data has been updated,OK" message can give users a lot of confidence. I appreciate this wasn't the question you asked but thought I'd mention it.
 
As I got lots of images and I want to perform the same Mouseup and Mousedown event on all those images. Can I write the following in a module and call it whenever a image is clicked so then we don't need to write the same code again and again for each image.
Code:
 Private Sub yourImageName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.yourImageName.Height = 500
Me.yourImageName.Width = 500
End Sub
----------------------------------------------------
Private Sub yourImageName_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.yourImageName.Height = 450
Me.yourImageName.Width = 450
End Sub

Thanks
 
put in public sub in a module:

public sub ExplodeImage(img As Access.Image)
img.Height=500
img.Width = img.Height
end sub

public sub ImplodeImage(img As Access.Image)
img.Height=450
img.Width = img.Height
end sub


on your image mouse up event:

Private Sub yourImageName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
ExplodeImage me.yourImageName
end sub

Private Sub yourImageName_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ImplodeImage me.yourImageName
end sub


better yet create a custome Class.
 
here i made the class for you, insert a new class module (named it clsImage) and paste the code:
Code:
Option Compare Database
Option Explicit

Public WithEvents img As Access.Image

Const event_hook As String = "[event procedure]"
Const small_icon_size As Long = 450
Const big_icon_size As Long = 500

Public Sub SetUp(i As Access.Image)
    Set img = i
    img.OnMouseDown = event_hook
    img.OnMouseUp = event_hook
End Sub

Private Sub Class_Terminate()
    Set img = Nothing
End Sub

Private Sub img_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    img.Height = big_icon_size
    img.Width = big_icon_size
End Sub

Private Sub img_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    img.Height = small_icon_size
    img.Width = small_icon_size
End Sub

on open event of your form

' if you have 3 image as button
private img1 as clsImage
private img2 as clsImage
private img3 as clsImage

private sub form_open(cancel as integer)
set img1 = new clsImage
set img2 = new clsImage
set img3 = new clsImage

img1.SetUp Me.yourImageControl1
img2.SetUp Me.yourImageControl2
img3.SetUp Me.yourImageControl3

end sub


on forms unload event:

private sub form_unload()
set img1 = nothing
set img2 = nothing
set img3 = nothing
end sub

That's all there is.
 
here's the enhanced class, centers the image when clicked.
Code:
Option Compare Database
Option Explicit

Public WithEvents img As Access.Image

Const event_hook As String = "[event procedure]"
Private small_icon_size As Long
Private big_icon_size As Long
Private OrigTop As Long
Private OrigLeft As Long
Public Sub SetUp(i As Access.Image, Optional BigSize As Long = 500, Optional SmallSize As Long = 450)
    Set img = i
    big_icon_size = BigSize
    small_icon_size = SmallSize
    
    img.OnMouseDown = event_hook
    img.OnMouseUp = event_hook
    
    OrigTop = i.Top
    OrigLeft = i.Left
    img.Height = big_icon_size
    img.Width = big_icon_size

End Sub

Private Sub Class_Terminate()
    Set img = Nothing
End Sub

Private Sub img_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    With img
        .Top = OrigTop
        .Left = OrigLeft
        .Height = big_icon_size
        .Width = big_icon_size
    End With
End Sub

Private Sub img_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    With img
        .Height = small_icon_size
        .Width = small_icon_size
        .Top = OrigTop + ((big_icon_size - small_icon_size) \ 2)
        .Left = OrigLeft + ((big_icon_size - small_icon_size) \ 2)
    End With
End Sub
 
arnelgp, I am getting the error message as below when I try to open the form.
 

Attachments

Last edited:
Try putting the Image into the Command Button.

Simon
 
on vba did you create a new class module and paste the code and named the module clsImage? or you just put it in the open event of your form?
 
Yes I created Class module and named it ClsImage. Not sure why this error is coming?
 
heres a a sample db. also i modify some codes in the class. maybe this will help you further.
 

Attachments

Users who are viewing this thread

Back
Top Bottom