How to set new embedded image for bound togglebutton = true

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JenniferM
    New Member
    • Oct 2010
    • 33

    How to set new embedded image for bound togglebutton = true

    I've got a series of toggle buttons that are bound to yes/no fields in a table. Right now, the picture for all of them in the properties section is set to BtnGray.bmp (which is embedded). I have another .bmp file (BtnBlue.bmp) that I want to display if the toggle button's state is true.

    My previous attempt to work around this problem (although I'm pretty sure this is NOT the way it should be done) involved creating an unbound toggle button, setting it's visibility to "No" and calling for that picture on the AfterUpdate event of the bound toggle button.

    In the code, IntSypmtPain is the bound toggle button, and ToggleBlue is the unbound button that contains the picture I want to use.

    Code:
    Private Sub IntSymptPain_AfterUpdate()
    
    If IntSymptPain.Value = True Then
        Me.IntSymptPain.Picture = Me.ToggleBlue.Picture
    End If
    
    End Sub


    I'm having 2 problems with my shoddy workaround:

    1. I'm getting an error:
    "Run-time error '2220': Microsoft Access can't open the file 'BtnBlue.bmp'."

    When I click debug, the line containing "Me.IntSymptPai n.Picture = Me.ToggleBlue.P icture" is highlighted.


    2. This only runs on the AfterUpdate event, so when I open a form to this specific record, it doesn't try to get BtnBlue.bmp until after the button has been clicked. I know I could add the code to multiple events within the form, but seeing as I have multiple toggle buttons, wouldn't that get a little ridiculous?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    1. Copy BtnBlue.bmp and BtnGray.bmp into the same Folder as your Database.
    2. Copy-N-Paste the following Sub-Procedure into the Form's Class Module, the Form containing the Toggle Buttons.
      Code:
      Private Sub SetTogglePicture(tgl As ToggleButton)
      Dim strPathToBMPs As String
      
      strPathToBMPs = CurrentProject.Path & "\"
      
      If tgl.Value Then
        tgl.Picture = strPathToBMPs & "BtnBlue.bmp"       'True
      Else
        tgl.Picture = strPathToBMPs & "BtnGray.bmp"       'False
      End If
    3. In the AfterUpdate() Event of each Toggle Button, call the Sub-Routine, passing to it the reference to the Toggle Button selected, as in:
      Code:
      Private Sub Toggle59_AfterUpdate()
        Call SetTogglePicture(Me![Toggle59])
      End Sub
    4. The appropriate *.BMP wil now be dynamically loaded into the Picture Property of a Toggle Button depending on its State.

    P.S. - There is another alternative using 2 Unbound Toggle Controls whose Visible Properties would be set to NO, and their Images would be Embedded as opposed to dynamically Loaded. I feel as though this approach is a little cumbersome, but it does have the distinct advantage that the Images are Embedded and simply transferred to other Toggle Buttons (no concerns as far as the *.bmps being moved or deleted, and generating Errors).

    Comment

    • JenniferM
      New Member
      • Oct 2010
      • 33

      #3
      Thanks!! This works great!

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Glad it works for you.

        Comment

        Working...