Developer

Create a reusable class component in VB6 to read INI data

Most VB6 developers store critical information, such as database names, in INI files. Find out how you can access the information faster by creating a reusable class.


Developers typically use INI files to store various types of application information, such as server and database names, so that they can access it later. To make it easier to access this INI data, you can create a simple reusable class. That way, you'll never have to copy and paste the subroutines that perform this functionality—you can just add the class file to your project.

Let's code
We'll start by creating a simple VB project that includes our reusable class component. Then, we'll examine the code that makes this technique work. You can grab the complete code for this article here.
  1. Fire up VB and start a new project.
  2. Add four command text boxes (lblServerName, lblDBName, lblDriver, and lblUID) and four labels (txtServerName, txtDBName, txtDriver , and txtUID).

  3. Your screen should look like Figure A.

    Figure A
    Creating the project

  4. Add the code from Listing A to Form_Load() event.
  5. Add a module MMain.bas and enter the code shown in Listing B.
  6. Add a class module and use the code shown in Listing C.

  7. Create a file named Settings.ini in the same directory as the project, using the code snippet shown in Listing D.
  8. Press [Ctrl][F5] to run the project. You should see a screen that resembles Figure B.

Figure B
The information retrieved from the INI file should appear in the appropriate text boxes.

How does this work?
In the Form_Load event, you create an instance of the CReadINI class and define a variable to store the location of the INI file. In this example, the location is in the App.Path (same location as the project). The name of the INI file is Settings.ini and it has to be concatenated to the value of the sLocation string. That’s why you check to make sure that the last character of the App.Path is /. If it’s not, you just add it as a last character.

Next, you pass the string sLocation and Settings.ini to m_oReadINI (an instance of CReadINI class). Also, you specify the name of the section used to read the data from. In this case it’s StartUp, and it’s the only one there, but you could have more.

Specify the key
Now, you need to specify the actual key for which the data should be returned:

  1. Assign a Key property of m_oReadINI to UID.
  2. Assign the result of a CReadINI class's method ReadINISettings, which returns the value found in the INI file under UID.
  3. Do the same thing for the rest of the items (Server, DBName, Driver).
  4. Set m_oReadINI to Nothing.

Ready for CReadINI.cls
Let's take a look at what happens in CReadINI.cls. In the general declaration section, you declare three private variables (m_sSection, m_sKey, m_sININame), which store the section of the INI file, the key to search for in the file, and the name of the INI file. You also define three Public properties to assign values to the private variables from the calling form. You need to create only Let properties since you assign data to the class object and don’t read data from it. Consequently, you omit Get properties.

Your class includes a method to read the data from the INI file and return it to the calling form (ReadINISettings). In ReadINISettings, you employ the API function GetPrivateProfileString (defined in module MMain.bas) to read INI data and to raise an error to the calling form in case there's an error in reading the data. (The class raises an error if there is a problem reading data from the INI file or if the file doesn’t exist.) This error is passed back to the to the calling function, that is, the form that called the method. Here we use the values we got from our Let properties (m_sSection, m_sKey, and m_sININame) to read the INI data for the specified section (m_sSection), key (m_sKey), and INI file (m_sININame).

The line lReturnVal = GetPrivateProfileString(m_sSection, m_sKey, "", sDestination, Len(sDestination), m_sININame) actually gets the value from the INI file. If lReturnVal returns a nonzero value, the API function successfully found the value. If lReturnVal returns zero, the API function could not find the value and some error occurred (for instance the INI filename is invalid). The trimmed value read from the INI file is returned back to the form.

Easy access
Once you've created this class component, you won't have to paste in subroutines to handle the task of reading information from INI files. Just add the class to your project to access the data you need. In future articles, I will explore other techniques you can use to simplify your code.

Advanced VB6 content
If you'd like to contribute some advanced VB6 articles, contact the editors.

 

Editor's Picks