Visual Basic Code of the Week (COTW)
Issue #63
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1998 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Late breaking information

View back issues of Code of the Week online at

See our web site for an exciting subscription offer. You do not want to miss this one.

In this Issue

This issue was designed for VB 4.0 and up. It is compatible with 16 bit and 32 bit versions of Visual Basic. Questions? Email us at

The complete source code appears near the end of this issue. Be sure not to miss it!

The class in this issue provides a very easy to use interface to the PrivateProfile API calls (the calls which allow you to read and write INI files). We know that many applications write information to the registry instead of INI files nowadays, but we feel INI files are still much easier to manage and can be modifed much easier with a text editor (if necessary).


There are several functions and properties in this class. There are three groups are properties to define what INI file and section to use, functions to read INI files and functions to write INI files.


Property Filename - Read/Write - Specifies the INI filename to use, for example: Ini.Filename = App.Path & "\TEST.INI"

Property Section - Read/Write - Specifies the section within a filename to use, for example: Ini.Section = "Paths"


There are 16-bit versions and 32-bit versions of the following functions. The only difference is that the 32-bit versions use Long values instead of Integers. We will show the 32-bit versions here. The full source code contains both versions.

Public Function ReadInt(sKey As String, iValue As Long, iDefault As Long) As Long
Public Function ReadString(sKey As String, sValue As String, sDefault As String) As Long
Public Function WriteInt(sKey As String, iValue As Long) As Long
Public Function WriteString(sKey As String, sValue As String) As Long



All functions return True upon success and False upon failure.

Sample Usage

Example 1 - How to retrieve a string from the INI file

    Dim Ini As New cIniFile

    Ini.Filename = App.Path & "\MYAPP.INI"

    ' get database path
    Ini.Section = "Paths"
    If Not Ini.ReadString("DatabasePath", gsDBPath, App.Path) Then
        MsgBox "Can not determine the DatabasePath, can not continue application."
        Exit Sub
    End If

Example 2 - How you can use INI files to save and restore the window position. The first routine should appear in the Form_Load event and the second should appear in the Form_Unload event

Private Sub Form_Load()
    Dim Ini As New cIniFile
    Dim lValue As Long

    Ini.Filename = App.Path & "\MYAPP.INI"
    Ini.Section = "Options"

    ' restore window position - force to upper left if no position exists
    Ini.ReadInt "WindowTop", lValue, 0
    Me.Top = lValue
    Ini.ReadInt "WindowLeft", lValue, 0
    Me.Left = lValue
    Set Ini = Nothing
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim Ini As New cIniFile

    Ini.Filename = App.Path & "\MYAPP.INI"
    Ini.Section = "Options"

    ' save window position
    Ini.Section = "Options"
    Ini.WriteInt "WindowTop", Me.Top
    Ini.WriteInt "WindowLeft", Me.Left
    Set Ini = Nothing
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945