INI File Class

By | 2019-09-20

This class module will allow you to easily work with INI files.

This will let you store settings and preferences for your program that are persistent between runs. You can download the attachment above, or copy and paste the below chunk of code to a new class module in your project.

Option Explicit

'API
Private Declare Function GetPrivateProfileInt Lib "kernel32.dll" Alias "GetPrivateProfileIntA" ( _
   ByVal lpApplicationName As String, _
   ByVal lpKeyName As String, _
   ByVal nDefault As Long, _
   ByVal lpFileName As String _
) As Long

Private Declare Function GetPrivateProfileString Lib "kernel32.dll" 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 "kernel32.dll" Alias "WritePrivateProfileStringA" ( _
   ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, _
   ByVal lpString As Any, _
   ByVal lpFileName As String _
) As Long

Private sFile As String

Public Sub Create(sFile_ As String)
   
   sFile = sFile
   
End Sub

Public Property Get Data(sSection_ As String, sKey_ As String) As Integer
   
   Data = GetPrivateProfileInt(sSection_, sKey_, -1, sFile)

End Property

Public Property Let Data(sSection_ As String, sKey_ As String, iData As Integer)
   
   Dim sData As String
   sData = iData_
   WritePrivateProfileString sSection_, sKey_, sData, sFile

End Property

Public Property Get Text(sSection_ As String, sKey_ As String) As String
   
   Dim sText As String
   Dim lResult As Long
   sText = String$(255, 0)
   lResult = GetPrivateProfileString(sSection_, sKey_, "", sText, Len(sText), sFile)
   If lResult = 0 Then
      Text = ""
   Else
      Text = Left(sText, InStr(sText, Chr(0)) - 1)
   End If

End Property

Public Property Let Text(sSection_ As String, sKey_ As String, sText As String)
   
   WritePrivateProfileString sSection_, sKey_, sText_, sFile

End Property

Attachments

FileUploadedSize
936-20190920-105359-CINI.cls9/20/2019 10:53:59 AM2270
Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.