Microsoft Access VBA Macro Programming

(Tina Sui) #1

x = GetPrivateProfileString("Parameters", "Path", "", s$, 256, "myini.ini")
MsgBox s$
MsgBox x
End Sub


The first line writes the INI file. If the file is not already there, it is automatically created. The
default location is in the Windows directory, although you can change this by placing a
pathname onto the filename.
The first parameter (Parameters) is the section of the INI file into which to write the new
string. The second parameter is the keyname or the entry to set—if set to Null, it will delete
all keys within this section. The third parameter is the value to write for the key. This is
"C:\temp\"—if set to Null, it will delete the existing string for that key. The fourth parameter
is the name of the INI file. You can use any suffix; you do not have to use an INI suffix. This
is quite useful if you want to hide some settings and wish to disguise your INI file—you can
give it a DLL or EXE suffix so it looks like a program file on casual inspection.
This now creates MYINI.INI. If you look in your Windows directory, you will see that the
file has been created. If you double-click it, it will be loaded into Notepad because it is a text
file, and should look like this:


[Parameters]
Path=C:\temp\


The next API call reads the path back in. However, before this can happen, you must
create a variable for it to be placed in.
TheSpace$command has been used to create a string of 256 spaces. Also, this variable
must be specified as a string; otherwise, errors will occur. The return value comes back as a
string, so it must be placed into a string variable.
GetPrivateProfileStringworks in a similar way toWritePrivateProfileString, but there
are more parameters to pass. The first and second parameters are the same as before and give
details of the section and the key to be read. The third parameter has a default value to return
if the entry is not found. The fourth parameter contains the variable name to which the result
will be passed. The fifth parameter contains the maximum number of characters to load into
the variable. The sixth parameter is the filename to search for.
The variable calling the API (x) will give the number of characters returned.S$will
contain the key, terminated by a Null character. If the keyname is left as Null, then all entries
in that section will be returned, each terminated with a Null character.
Both these API calls are very forgiving in their operation. If the file is not there, it is
created. If the key is not there, it is created. If the file does not exist when you use the read
command, then a Null value will be returned.
This method is often used in programs to keep track of user settings so that when the user
enters the program subsequently, it is set to that user’s personal settings.
Microsoft no longer uses INI files; Windows uses keys in the Registry to record this
information.


Chapter 20: API Calls 257

Free download pdf