Why Do You Need the API

VBA is a very powerful language, but you can only control a small part of the operating system with its built-in functions. One of the best features of VBA is its extensibility; that is, you can extend its capabilities in a variety of ways—one of which is by using the API.

For example, VBA provides several built-in functions for manipulating the Windows Registry, but these functions only let you use one small part of the registry set aside for VBA. To access the remainder of the registry, you need to use the API.

Similarly, to retrieve and manipulate disk drive, printer, or system resource settings, you'll need the API. If you'd like your Access applications to do more than just beep, the sndPlaySound API function will let you play sound effects or music. You can even control the transparency of your Access forms using several API functions in conjunction.

The following example puts an icon in a form's title bar; something you can't do using standard VBA. Place the following code into a standard module.

Public 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

Public Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" (ByVal hWnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, _ LParam As Any) As Long

'Image type constants Public Const IMAGE_BITMAP = 0 Public Const IMAGE_ICON = 1 Public Const IMAGE_CURSOR = 2 Public Const IMAGE_ENHMETAFILE = 3

'un2 Flags

Public Const LR_DEFAULTCOLOR = &H0 Public Const LR_MONOCHROME = &H1 Public Const LR_COLOR = &H2 Public Const LR_COPYRETURNORG = &H4 Public Const LR_COPYDELETEORG = &H8 Public Const LR_LOADFROMFILE = &H10 Public Const LR_LOADTRANSPARENT = &H2 0 Public Const LR_DEFAULTSIZE = &H4 0 Public Const LR_LOADMAP3DCOLORS = &H1000 Public Const LR_CREATEDIBHeader = &H200 0 Public Const LR_COPYFROMRESOURCE = &H40 00 Public Const LR_SHARED = &H8000

'Message params

Public Const WM_GETICON = &H7F Public Const WM_SETICON = &H80 Public Const ICON_SMALL = 0 Public Const ICON_BIG = 1

'Default image size for the Access Titlebar Public Const IMG_DEFAULT_HEIGHT = 16 Public Const IMG_DEFAULT_WIDTH =16

Public Sub SetFormIcon(hWnd As Long, strIcon As String) Dim hIcon As Long Dim lngReturn As Long hIcon = LoadImage(0&, strIcon, IMAGE_ICON, IMG_DEFAULT_WIDTH, _ IMG_DEFAULT_HEIGHT, LR_LOADFROMFILE)

If hIcon <> 0 Then lngReturn = SendMessage(hWnd, WM_SETICON, ICON_SMALL, ByVal hIcon) End If End Sub

Then create a new form and add the following code to the form's Load event, making sure to change the c:\myIcons\myIco.ico path to that of an icon file that exists on your computer.

Private Sub Form_Load()

SetFormIcon Me.hWnd, "C:\myIcons\myIcon.ico" End Sub

Now open the form to see the icon appear in the form's title bar.

With the APIs at your disposal, you can now control a significant portion of the Windows operating system and almost everything within it. Let's begin by discussing how the API works.

0 0

Post a comment