Microsoft Access VBA Macro Programming

(Tina Sui) #1
You can check the values by going into Windows Explorer (right-click the Windows Start
button in the bottom-left corner of the screen and select Explore), selecting the root of C:,
right-clicking this, and choosing Properties. A pie chart will appear showing usage of your C:
drive, and the values for total space and free space will agree exactly to numbers displayed in
the preceding code.

Reading from and Writing to INI Files


API calls can also be used for reading from and writing to INI files. INI files have now been
superseded by the Windows Registry in terms of maintaining program parameters and
settings, but they are still useful as a simple way of keeping information.
API calls also exist for the Windows Registry, but you need a very good knowledge of
what you are doing if you are going to use API calls to alter the Registry. If you make a
mistake, you will end up having to reinstall a particular program related to that setting, and at
worst you may damage the integrity of Windows and end up having to reinstall Windows.
One of their useful functions is creating INI files for your program. INI files are a means
of storing settings for your program, such as control settings that you want to be sticky or the
details of user settings for individual applications. Variables and properties in VBA only hold
their values while the program is present. Once you close down, all is lost, and everything
returns to default.
An example might be a text box that holds a directory pathname set by the user. The user
might put in a different pathname, but when the program is closed, it reverts to the default on
reloading. This is irritating for the user, who has to keep inputting the pathname.
The INI file holds this information locally so it can be retrieved when the program is
loaded next time.
Two declarations can be used for your INI file. (Other declarations are available for
reading and writing to INI files, but you will use these in the example.)
Private Declare Function GetPrivateProfileString Lib "kernel 32 " Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As _
String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare Function WritePrivateProfileString Lib "kernel 32 " Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As _
Long

These need to be added into the declarations section of a module (at the top of the module
page). They set up references to the kernel32.dll file and describe how the parameters will be
passed. You can then use code to pass the parameters and write to the file:
Sub Test_INI()
x = WritePrivateProfileString("Parameters", "Path", "C:\temp\", "myini.ini")
s$ = Space$( 256 )

256 Microsoft Access 2010 VBA Macro Programming

Free download pdf