How could I use VBA to change a button's pictu

2019-06-14 10:32发布

I'm trying to get a button to change from the "Arrow Left" to "Arrow Right" picture when I click it, but I'm not sure how to assign the images through VBA. I tried Me!btnCollapseUnscheduled.Picture = "Arrow Left", but I get an error message this way.

3条回答
Ridiculous、
2楼-- · 2019-06-14 11:08

In Access 2010 or later, you can store images in the MSysResources system table. Then choose Shared for your command button's Picture Type property, and select one of those shared images.

Afterward, it's easy to toggle between the shared images from the command buttons's click event. I named my command button "cmdArrow" and the shared images "Arrow Left" and "Arrow Right" ...

Option Compare Database
Option Explicit

Private Sub cmdArrow_Click()
    Dim strDirection As String

    If Me!cmdArrow.Picture Like "*Left" Then
        strDirection = "Right"
    Else
        strDirection = "Left"
    End If
    Me!cmdArrow.Picture = "Arrow " & strDirection
    Me!txtImage.Value = Me!cmdArrow.Picture
End Sub

Private Sub Form_Load()
    Me!txtImage.Value = Me!cmdArrow.Picture
End Sub

Screenshots:

command button with Arrow Left image

command button with Arrow Right image

The biggest challenge was loading the images into MSysResources. I created a throwaway form and added a command button. Next I chose Embedded as the button's Picture Type property and selected Arrow Left from the available choices. Then I changed Picture Type from Embedded to Shared, which added a row to MSysResources with the image data in an attachment field and my command button's name in the Name field. So I opened the table, changed Name to "Arrow Left" and closed the table. Note MSysResources will not be visible in the Navigation pane unless you enable "Show System Objects" in the Navigation Options.

I repeated those steps for "Arrow Right". It may sound fiddly, but it's not really a huge hurdle.

查看更多
Fickle 薄情
3楼-- · 2019-06-14 11:18

The trick is to use the full file path of your icon.

MS-Access might be using some icon map for its native icons, so you might not have much luck getting a path to one of those.

If you definitely can't find the path to the native MS-Access icons, you could always just do a google image search for arrow icons (specify exact size of 16 x 16 pixels).

You can then just save these icons to a folder of your choosing and then use those paths in the following illustrations.

If you just want the button to change its image on the first click and stay that way thereafter:

  1. Make sure the button does not have a picture attached in Design View.
  2. Assign the default image using its full path to your command button using the form's Open event:
Private Sub Form_Open(Cancel As Integer)

    Me.cmdButton.Picture = "C:\myFolder\myDefaultIcon.png"

End Sub
  1. Then in the button's click event, assign the .picture property to the icon path you want to change it to:
Private Sub cmdButton_Click()

    Me.cmdButton.Picture = "C:\myFolder\myChangedIcon.png"

End Sub

If you just want the button to toggle between 2 images on each click:

  1. Use a toggle button control instead of a command button control
  2. Make sure the button does not have a picture attached in Design View.
  3. Write a sub that will handle the different on/off states of your toggle button:
Public Sub togImg()

    If _
        Me.togButton = -1 _
    Then

        Me.togButton.Picture = "C:\myFolder\myChangedIcon.png"

    Else

        Me.togButton.Picture = "C:\myFolder\myDefaultIcon.png"

    End If

End Sub
  1. Call your sub from both the form's Open event and the toggle button's Click event:
Private Sub Form_Open(Cancel As Integer)

    togImg

End Sub

Private Sub togButton_Click()

    togImg

End Sub
查看更多
地球回转人心会变
4楼-- · 2019-06-14 11:26

You could create two buttons where only one is visible at a time, both calling the same code.

When one is clicked, unhide the other, set focus to this, then hide the first button. And vice-versa.

查看更多
登录 后发表回答