How could I use VBA to change a button's picture to one of the embedded default images?

℡╲_俬逩灬. 提交于 2019-12-23 04:45:43

问题


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.


回答1:


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:

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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/33357544/how-could-i-use-vba-to-change-a-buttons-picture-to-one-of-the-embedded-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!