Stuart McCall's Microsoft Access Pages - Ini Files
Home
Basic storage & retrieval of single items
Private Declare Function GetPrivateProfileStringA Lib "Kernel32" (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 WritePrivateProfileStringA Lib "Kernel32" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private m_iniFile As String
|
Private Sub InitIni()
'Ini File is created in current database path
'with same name as database & .ini
If Len(m_iniFile) = 0 Then
With CurrentProject
m_iniFile = .Path & "\" & Left$(.Name, InstrRev(.Name, ".")) & "ini"
End With
End If
End Sub
Public Function iniRead(ByVal Item$, Optional Section = "Settings") As String
Const buflen& = 255
Dim x As Long
Dim buffer As String * buflen
InitIni
x = GetPrivateProfileStringA(Section, Item, "", buffer, buflen - 1, m_iniFile)
iniRead = Left(buffer, x)
End Function
Public Sub iniWrite(ByVal Item$, ByVal Value$, Optional Section = "Settings")
InitIni
WritePrivateProfileStringA Section, Item, Value, m_iniFile
End Sub
|
Usage:
iniWrite "Default Form to load", "frmMainMenu", "Forms"
results in an ini file containing:
[Forms]
Default Form to load=frmMainMenu
To read back the value:
Debug.Print iniRead("Default Form to load", "Forms")
results in the immediate window:
frmMainMenu
Obtain an Ini file's sections as a delimited list
Public Function iniGetSections(ByVal FileName As String) As String
'Obtains all section names from ini file FileName
'Returns string delimited with ';'
'Use as combo/listbox value list
Dim r$,f%, b$
On Error GoTo iniGetSections_Err
'
f = FreeFile
Open FileName For Input Access Read As f
Do Until EOF(f)
Line Input #f, b
If InStr(1, b, "[") = 1 Then
r = r & UnBracket(b) & ";"
End If
Loop
Close f
If Right$(r, 1) = ";" Then r = Left$(r, Len(r) - 1)
iniGetSections = r
iniGetSections_Exit:
Exit Function
iniGetSections_Err:
MsgBox Err.Description
Resume iniGetSections_Exit
End Function
Private Function UnBracket(ByVal s$) As String
s = Replace(s, "[", "")
s = Replace(s, "]", "")
UnBracket = Replace(s, "].[", ".")
End Function
|
Does an Ini file's section exist?
Public Function iniSectionExists(Sect, File) As Boolean
Dim f%, b$
f = FreeFile
Open File For Input Access Read As f
Do Until EOF(f)
Line Input #f, b
If InStr(1, b, "[") = 1 Then
If b = "[" & Sect & "]" Then
iniSectionExists = True
Exit Do
End If
End If
Loop
Close f
End Function
|
Read/write an Ini file's section using an array
Private Declare Function GetPrivateProfileSectionA Lib "Kernel32" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileSectionA Lib "Kernel32" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
|
Public Function iniReadSectionArray(Section, File)
Const buflen& = &H800
Dim buffer As String * buflen, x&
x = GetPrivateProfileSectionA(CStr(Section), buffer, buflen - 1, CStr(File))
iniReadSectionArray = Split(Left(buffer, x - 1), vbNullChar)
End Function
Public Sub iniWriteSectionArray(Section, Items, File)
WritePrivateProfileSectionA CStr(Section), Join(Items, vbNullChar) & vbNullChar, CStr(File)
End Sub
|
Usage:
Dim a As Variant
a = iniReadSectionArray("Settings", "c:\MyFolder\MyIni.ini")
For Each item In a
Debug.Print item
Next
iniWriteSectionArray "Settings", a, "c:\MyFolder\MyIni.ini"