How to Create a Configuration.INI Files in VB6

In this example shows only a simple form that saves and retrieves value from the CONFIG.INI file.

After reading this, you can use your own creativity on how you're going to implement this on your project.


To start with, On your Form add the following controls and arrange them according to the image above.
(1) TextBox - Change the Name property to "txtSetting"
(2) Command Button - Change the Caption and Name Properties to the following:
                                      Save:         Change the Name property to "cmdSave"
                                      Retrieve:   Change the Name property to "cmdRetrieve"
(5) Label - For the two labels we'll be using to retrieve the values. Remove the default caption and change the Name Properties to lblName and lblTime.
Now, Copy the Form Codes below and Paste on the Form1 code window of your VB6 Project.
Form Codes:

Option Explicit

Dim ConfigFile As String

Private Sub cmdRetrieve_Click()
    'Set the Configuration File Name
    ConfigFile = App.Path & "\CONFIG.INI"
    
    lblName.Caption = GetINISetting("USER", "NAME", ConfigFile)
    lblTime.Caption = GetINISetting("USER", "TIME", ConfigFile)
    
End Sub

Private Sub cmdSave_Click()
    'Set the Configuration File Name
    ConfigFile = App.Path & "\CONFIG.INI"
    
    'Check for empty values
    If txtSetting.Text = "" Then
        MsgBox "Please type your name on the textbox provided.", vbCritical, "Settings"
        txtSetting.SetFocus
        Exit Sub
    End If
    
    'Put the name and the time into the config file
    PutINISetting "USER", "NAME", txtSetting.Text, ConfigFile
    PutINISetting "USER", "TIME", Format(Now, "yyyy-mm-dd hh:mm:ss AMPM"), ConfigFile
    
    MsgBox "Settings have been saved.", vbInformation, "Settings"
    
End Sub

You can save copy and paste the following code in a standard module and save it as settings.bas or give it a name of your own. The WritePrivateProfileSetting API Function creates the Configuration file name if it doesn't exists.
Module (settings.bas)

Option Explicit

'API Function to read information from INI File
Public Declare Function GetPrivateProfileString Lib "kernel32" _
    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

'API Function to write information to the INI File
Private Declare Function WritePrivateProfileString Lib "kernel32" _
    Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpString As Any, _
    ByVal lpFileName As String) As Long

'Get the INI Setting from the File
Public Function GetINISetting(ByVal sHeading As String, _
    ByVal sKey As String, _
    sINIFileName) As String
    
    Const cparmLen = 50
    Dim sReturn As String * cparmLen
    Dim sDefault As String * cparmLen
    Dim lLength As Long
    lLength = GetPrivateProfileString(sHeading, _
            sKey, _
            sDefault, _
            sReturn, _
            cparmLen, _
            sINIFileName)
            
    GetINISetting = Mid(sReturn, 1, lLength)
End Function

'Save INI Setting in the File
Public Function PutINISetting(ByVal sHeading As String, _
    ByVal sKey As String, _
    ByVal sSetting As String, _
    sINIFileName) As Boolean
    
    Const cparmLen = 50
    Dim sReturn As String * cparmLen
    Dim sDefault As String * cparmLen
    Dim aLength As Long
    aLength = WritePrivateProfileString(sHeading, _
        sKey, _
        sSetting, _
        sINIFileName)
        
    PutINISetting = True
End Function

This will be how the configuration file looks like if we open it with a text editor.

Comments

Post a Comment

Popular posts from this blog

How to Set Windows Form Always on Top of Other Applications in VB6

How to Minimize/Send an App Icon to System Tray in Visual Basic 6