• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 98
  • Last Modified:

putting an icon in a form

I had this question after viewing type mismatch calling function.
I have  picked a solution from EE to put an icon on a form in ms/access. I have a solution the previously worked which is:

(1) putting the logo in the database folder (e.g. the file "logoFinal32.ico")
(2) put the following code at form load:

    strFormIcon = "logoFinal32.ico"
    SetFormIcon Me.hwnd, Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name))) & "\" & strFormIcon

(3) have following functions  and definitions in a module:

Option Compare Database

Option Explicit

Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Declare PtrSafe Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As LongPtr, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As LongPtr
Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr

Private Const WM_SETICON = &H80
Private Const IMAGE_ICON = 1
Private Const LR_LOADFROMFILE = &H10
Private Const SM_CXSMICON As Long = 49
Private Const SM_CYSMICON As Long = 50


Public Function SetFormIcon(hwnd As Long, strIconPath As String) As Boolean

    Dim lResult As LongPtr
    Dim X As Long, Y As Long
    Dim lIcon As LongPtr

    X = GetSystemMetrics(SM_CXSMICON)
    Y = GetSystemMetrics(SM_CYSMICON)
    lIcon = LoadImage(0, strIconPath, 1, X, Y, LR_LOADFROMFILE)
    lResult = SendMessage(hwnd, WM_SETICON, 0, ByVal lIcon)

End Function


my file has a 32x32 pixel resolution

but the form icon remains

can anyone help?
0
João serras-pereira
Asked:
João serras-pereira
  • 6
  • 5
  • 2
2 Solutions
 
NorieCommented:
How/when are you running the code to set the icon?
0
 
João serras-pereiraAuthor Commented:
at form load...
0
 
NorieCommented:
Can you post the exact code you are using?
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
João serras-pereiraAuthor Commented:
yeap. It is just above, on the starting of this question
0
 
NorieCommented:
Can you post the whole code for the form's Load event?

Then we can see if there is a problem there.
0
 
João serras-pereiraAuthor Commented:
sure!

here it goes:

Private Sub Form_Load()

    Dim strFormIcon As String

    'DoCmd.ShowToolbar "Ribbon", acToolbarNo
    DoCmd.ShowToolbar "Ribbon", acToolbarYes

    'DoCmd.NavigateTo "acNavigationCategoryObjectType"
    'DoCmd.RunCommand acCmdWindowHide
    'DoCmd.RunCommand acCmdWindowShow
    DoCmd.OpenForm "_commonVariables", , , , , acHidden
   
    Forms![_commonVariables]![latGPS] = 12.29027777777
    Forms![_commonVariables]![lgtGPS] = -15.3863888888
    Forms![_commonVariables]![raioLimite] = 1000
    Forms![_commonVariables]![userID] = ""
    Call logMe("load form [00 base]", "")
   
    On Error Resume Next
    strFormIcon = "logoFinal32.ico"
    SetFormIcon Me.hwnd, Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name))) & "\" & strFormIcon
   
   
End Sub




and the full module code is:


ption Compare Database

Option Explicit

Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Declare PtrSafe Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As LongPtr, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As LongPtr
Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr

Private Const WM_SETICON = &H80
Private Const IMAGE_ICON = 1
Private Const LR_LOADFROMFILE = &H10
Private Const SM_CXSMICON As Long = 49
Private Const SM_CYSMICON As Long = 50


Public Function SetFormIcon(hwnd As Long, strIconPath As String) As Boolean

    Dim lResult As LongPtr
    Dim X As Long, Y As Long
    Dim lIcon As LongPtr

    X = GetSystemMetrics(SM_CXSMICON)
    Y = GetSystemMetrics(SM_CYSMICON)
    lIcon = LoadImage(0, strIconPath, 1, X, Y, LR_LOADFROMFILE)
    lResult = SendMessage(hwnd, WM_SETICON, 0, ByVal lIcon)

End Function





and the ico file is uploaded on the message (I have changed the extension to png as EE does dot accept to upload ICO files. I am using ICO and not png because, as long as I know, the ms/access forms does not accept PNG....
logofinal32.png
0
 
NorieCommented:
I've just tested the code with an icon from another source and it works OK.

Are you sure the icon you are trying to use is suitable?

That seems to be the main problem people have with this code.

I used one of the icons from the example database in post #8 here.
0
 
João serras-pereiraAuthor Commented:
Well - I should be okay but I am going to try with a smaller (16x16) version.. as well as with other bitmaps

did you try with my bitmap?
0
 
NorieCommented:
I tried with the image you uploaded as it was, ie a png file, and after saving as bitmap but neither  worked I'm afraid.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
To reinforce what Norie said:

Access is very temperamental in regard to images. Some that you feel should work simply will not. As Norie has suggested, use a different icon, or try converting the icon to a different format (like .bmp). There are several online converters that will do this, or you could use something like SnagIt's editor to do it for you.

In other words - your code works fine. Your image is not suitable for Access. You cannot change the behavior of Access, so your only avenue is to change the image you're trying to use.
0
 
João serras-pereiraAuthor Commented:
Hi -

My image is NOT a PNG. It is a .ICO had to change the extension "the ico file is uploaded on the message (I have changed the extension to png as EE does dot accept to upload ICO files. I am using ICO and not png because, as long as I know, the ms/access forms does not accept PNG...."

But Ok I accept the solution and close the question...
thanks for the help!
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
My image is NOT a PNG. It is a .ICO had to change the extension
That's not the point ... as Norie and I have mentioned, Access simply does not accept certain image files, regardless of their format. In other words, MyImage.png may work perfectly fine, but MyOtherImage.png will not.

Try changing the format of the image file, or use a different image file. The one you're trying to use simply won't work.
0
 
João serras-pereiraAuthor Commented:
OK!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now