ACC2000: How to Associate a Custom Icon with a Form (304264)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q304264
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

In Microsoft Access, there is no built-in way to associate a custom icon for a form; however, this article demonstrates how to change a form's icon by using application programming interface (API) calls.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Sample Code

To use API calls to set a custom icon for a form, follow these steps.

NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive an error message that there is a duplicate procedure name, remove or comment out the declaration statements in your code.
  1. Create a module, and then type or paste the following code in the declarations section:
    Private Declare Function LoadImage Lib "user32" _
       Alias "LoadImageA" _
       (ByVal hInst As Long, _
       ByVal lpsz As String, _
       ByVal un1 As Long, _
       ByVal n1 As Long, _
       ByVal n2 As Long, _
       ByVal un2 As Long) _
       As Long
       
    Private Declare Function SendMessage Lib "user32" _
       Alias "SendMessageA" _
       (ByVal hWnd As Long, _
       ByVal wMsg As Long, _
       ByVal wParam As Long, _
       LParam As Any) _
       As Long
       
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    
    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 lIcon As Long
        Dim lResult As Long
        Dim X As Long, Y As Long
        
        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
    					
  2. To set the icon, type the following code in the form's module (substitute the correct drive and path for your custom icon):
    Private Sub Form_Open(Cancel As Integer)
       SetFormIcon Me.hWnd, "C:\MyIcon.ico"
    End Sub
    					

REFERENCES

For more information about declaration APIs, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type declare statement in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto KB304264