Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 27: Using the Windows Application Programming Interface


963


Although many vendors still use application-specific .ini files for their applications, more recent
versions of Windows use the System Registry for most settings. The Win32 API comes with every-
thing needed to control System Registry settings.


However, you may find it much simpler to use .ini files for storing persistent information (such
as configuration data) needed by your Access applications. One huge advantage that .ini files
have over the System Registry is that a user can use Notepad or Word to change the contents of an
.ini file, and changing an application’s .ini file won’t affect any other application on the
computer.


This section demonstrates the use of .ini functions for Win32.


GetPrivateProfileStringA


GetPrivateProfileStringA function retrieves a value from a private (application-specific)
.ini file.


Declare Function apiGetPrivateProfileString _
Lib “Kernel32” _
Alias “GetPrivateProfileStringA”( _
ByVal lpszSection As String, _
ByVal lpszKey As String, _
ByVal lpszDefault As String, _
ByVal lpszReturnString As String, _
ByVal dwReturnSize As Long, _
ByVal lpszFilename As String) As Long

It is passed the section, key, and .ini filename and retrieves the value for the key. If a Null value
is passed as a key, all the entries for the section are retrieved. If a specified key is not found, the
value passed as lpszDefault is returned. If the function is successful, it returns the number of
characters copied into the string buffer lpszReturnString. Sections, keys, and values are illus-
trated in the following examples.


[section]
key=string

The next example uses the CurentProject.Path property to retrieve the path for Access and
uses it as the path for the test.ini file. It then uses the GetPrivateProfileStringA API
function to retrieve a string value for the AppTitle key.


Function GetPrivateProfileString() As String
Dim Section As String
Dim KeyName As String
Dim Default As String
Dim ReturnBuffer As String
Dim Filename As String
Dim BufferSize As Long
Dim RetVal As Long
Dim IniPath As String
Free download pdf