Solved

putting an icon in a form

Posted on 2016-11-28
13
55 Views
Last Modified: 2016-12-21
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 33

Expert Comment

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

Author Comment

by:jirdeaid
ID: 41905297
at form load...
0
 
LVL 33

Expert Comment

by:Norie
ID: 41905308
Can you post the exact code you are using?
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

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

Expert Comment

by:Norie
ID: 41905351
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
ID: 41905377
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
 
LVL 33

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 41905511
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
ID: 41905537
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
ID: 41905545
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
ID: 41905555
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
ID: 41905618
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
ID: 41905649
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
ID: 41905682
OK!!!
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

733 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