Solved

putting an icon in a form

Posted on 2016-11-28
13
19 Views
Last Modified: 2016-11-29
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
Comment
Question by:jirdeaid
  • 6
  • 5
  • 2
13 Comments
 
LVL 33

Expert Comment

by:Norie
Comment Utility
How/when are you running the code to set the icon?
0
 

Author Comment

by:jirdeaid
Comment Utility
at form load...
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Can you post the exact code you are using?
0
 

Author Comment

by:jirdeaid
Comment Utility
yeap. It is just above, on the starting of this question
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Can you post the whole code for the form's Load event?

Then we can see if there is a problem there.
0
 

Author Comment

by:jirdeaid
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 33

Assisted Solution

by:Norie
Norie earned 250 total points
Comment Utility
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
 

Author Comment

by:jirdeaid
Comment Utility
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
 
LVL 33

Expert Comment

by:Norie
Comment Utility
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
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
Comment Utility
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
 

Author Closing Comment

by:jirdeaid
Comment Utility
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
 
LVL 84
Comment Utility
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
 

Author Comment

by:jirdeaid
Comment Utility
OK!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now