Images of Attachment type field
Last class we saw that we can use external images in the ribbons, using the attributes image and getimage. We used a local folder to store those images. What I’ll show you in this article is an alternative to store these images in a local table, using an attachment type field.
Nothing changes related to the XML code. What changes is the way of extracting images by the VBA code, that are now at a table.
Remember that, to use the attribute image of the controls, you must use the attribute loadImage of the tag CustomUI, which calls the function fncLoadImage.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="fncRibbon" loadImage="fncLoadImage">
... <button id = "bt1" label = "Feed" image="feed.png" size="large" onAction = "fncOnAction" /> ...
</customUI>
To load the images stored at a folder, we use the LoadPicture method of the Access or the function LoadImage, to use PNG and ICO images.
Check the function fncLoadImage used to load the images from a folder:
Public Sub fncLoadImage(imageId As String, ByRef Image) Dim strPath As String strPath = CurrentProject.Path & "\images\" If InStr(imageId, ".png") > 0 Or InStr(imageId, ".ico") > 0 Then Set Image = LoadImage(strPath & imageId) Else Set Image = LoadPicture(strPath & imageId) End If End Sub
The point is: how can we extract the Attachment type Field images from a local table? We can do it in two different ways:
The first way is to extract the images directly from the attachment type field of a Form that is linked with the table, using the method PictureDisp
The second way is to extract the image of the attachment type field , directly from the table, to a temporary folder, using the method SaveToFile
We will use both ways
See the code used to load, at the ribbon, the images extracted from an Attachment type Field, of a hidden form. This form is linked to the table that contains the images. Read carefully the comments in green!
Option Compare Database Dim attAnexo As Attachment '---------------------------------------------------------------------------- Public Sub fncLoadImage(imageId As String, ByRef Image) ‘Check if the form fmImgRibbons is open. If Not CurrentProject.AllForms("frmImgRibbons").IsLoaded Then 'Open form to just read and hidden. DoCmd.OpenForm "frmImgRibbons", acNormal, , , acFormReadOnly, acHidden 'Change the attached type field image of the form to the variable attAnexo Set attAnexo = Forms("frmImgRibbons").Controls("Images") End If 'Load images JPG, BMP, or Gif 'PictureDisp extracts Attachment type Field images of the form. Set Image = attAnexo.PictureDisp(imageId) End Sub
Remember we can’t load images PNG or ICO directly in the ribbon? We still using the function LoadImage, that transforms these images in BMP. To use this function, the image must be in a local folder. The alternative is to copy the Attachment type Field image from the table to a local folder. This image, saved at a temporary folder, goes to the LoadImage function, that will use and give it to the ribbon. After this treatment, the image is deleted from the temporary folder.
Check the code used to copy the Attachment type Field image from a table, to a temporary folder:
Public Function fncExtractImage(strImageName As String) As String Dim strPath As String Dim rsParent As DAO.Recordset Dim rsChild As DAO.Recordset2 Dim flData As Field2 Dim flName As Field2 strPath = CurrentProject.Path & "\temp" Set rsParent = CurrentDb.OpenRecordset("tblImagesRibbons") Set rsChild = rsParent.Fields("imageRibbon").Value Set flData = rsChild.Fields("filedata") Set flName = rsChild.Fields("Filename") 'Check if the temporary folder temp exists. If not, creates it and put 'in hidden mode. If Len(Dir(strPath, vbDirectory + vbHidden) & "") = 0 Then FileSystem.MkDir (strPath) FileSystem.SetAttr strPath, vbHidden End If 'Does a loop searching for the image. Do While Not rsChild.EOF If flName.Value = strImageName Then 'Saves Attachment type Field image in the temporary folder. flData.SaveToFile (strPath) Exit Do End If rsChild.MoveNext Loop Set flName = Nothing Set flData = Nothing Set rsChild = Nothing Set rsParent = Nothing 'The function gives the name and the pacho f the saved file, that will 'be given the function LoadImage fncExtractImage = strPath & "\" & strImageName End Function
When the image is saved at the temporary folder, the function LoadImage will treat it.
Observe the complete function fncLoadImage. Read carefully the comments in green.
Option Compare Database Dim attAnexo As Attachment '----------------------------------------------------------------------- Sub fncLoadImage(imageId As String, ByRef Image) Dim strPath As String ‘Verify if the form fmImgRibbons is open. If Not CurrentProject.AllForms("frmImgRibbons").IsLoaded Then 'Open form to just read and hidden. DoCmd.OpenForm "frmImgRibbons", acNormal, , , acFormReadOnly, acHidden 'Change the attached field form variable to attAnexo Set attAnexo = Forms("frmImgRibbons").Controls("Images") End If 'Verify if the image has the extension PNG or ICO to apply the 'transformation function LoadImage If InStr(imageId, ".png") > 0 Or InStr(imageId, ".ico") > 0 Then 'Give to the variable the local and the name of the PNG or ICO image, 'saved in the temporary folder. strPath = fncExtractImage(imageId) 'Transforms the PNG or ICO image into BMP, and puts in the ribbon. Set Image = LoadImage(strPath) 'Deletes the image from the temporary folder Temp FileSystem.Kill strPath Else 'Load images JPG, BMP ou GIF Set Image = attAnexo.PictureDisp(imageId) End If End Sub
Download the example, where you can find the code used here, and watch the vídeo, to check more details.
Video Class:
In this vídeo class you will have a brief presentation about the use of external images stored at a local table, and will see a presentation about the example file, that uses the gallery control, at runtime.
Links | Subjects |
---|---|
Brief presentation of MontaRibbons Structural concept of the XML used How to disable all the upper ribbon of the Access Using images from the Office’s gallery Using internal controls of the Office Customizing the quick toolbar Customizing the Office Button How to create a custom ribbon | |
Setting the control splitButton Setting the control menu Differences between the controls splitButton and menu Exporting the ribbons for your applications How to give functionality to the buttons on the ribbon |
|
Changing the controls at runtime, using the attributes gets How to configure the ribbon for the language exchange How to hide / disable the buttons on the ribbon, depending on the User logged |
|
Using external images (GIF, JPEG e PNG) | |
FAQ | |
Combobox and Dropdown, in practice | |
All the details about how to purchase |