As a general rule of thumb, I would never set about trying to write my own API call; I simply collect API calls that are known to work, and generally cut and paste them with few, if any, modifications. When you're calling external API functions you are no longer working in VBA's wonderfully safe environment. One simple typo can cause you to GPF (general protection fault), crash your app, and waste contless development hours trying to figure out what's up. Instead, find code that already has a good wrapper function around the API call you need, and tuck it away in a code library somewhere for future reference.
That said, there are some interesting resources out there...
The dlls that form the Windows API are not wrapped in type libraries, so you can't reference them in an Access app, and hence can't view their function definitions via the Object Browser. The obvious question then, is how do you know what the available API functions are?
Both the MS Office Developer Edition (ODE) and VB6 include a text file called WIN32API.TXT that contains VBA Declare statements for most of the functions in the Windows API (since VBA can't reference the relevant dlls [Kernel32.dll, User32.dll, Comdlg32.dll, etc.], the Declare statements are what tell VBA where to find a particular DLL function and how to call it). If you don't own either the ODE or VB6, you can download WIN32API.TXT for free from MS @:
(watch word wrap)
In additioin to WIN32API.TXT, the ODE and VB6 come with a utility exe called the API Viewer. APILOAD.exe is a VB6 add-in but works fine as a stand-alone exe. See:
(look under the "Accessing the Microsoft Windows API" node in the MSDN treeview for a screen capture of the API Viewer).
The API Viewer allows you to load WIN32API.TXT into a fairly klunky GUI, write the Declares, Constants, and User-defined type definiftions you need to a Textbox, then paste the completed API call into your app. It also lets you convert WIN32API.TXT into an Access database. Frankly, I find it easier to simply open WIN32API.TXT in Notepad/Wordpad and search for a function I'm interested in.
Then once you locate the Declare you want, you need to know what it means, and what parameters it requires!! For that, you need one or all of the below:
- Somebody elses (hopefully commented) code [recommended]
- WIN32 documentation (the MSDN library CD's contain the MS Platform SDK and are the best source)
- several good books.
Some of the better Access books have good chapters on API functions:
Getz, et al - Access 2002 Developer's Handbook, Desktop Edition
F. Scott Barker - Microsoft Access 2002 Power Programming
There are also some good books on the Winows API geared toward VB
developer's, but the VB
Declares and wrapper functions are identical in VBA. The best in terms of an exhaustive reference is:
Dan Appleman's Visual Basic Programmer's Guide to the Win32 API
I've also worked with:
Visual Basic 6 Win32 API Tutorial - Jason Brock (Wrox)
Win32 API Programming with Visual Basic - Steven Roman (O'reilly)