Karl Blessing
Programmer
This is what I have written so far, it does get back all the sections, and their values below them, which I soon hope to put each section into a collection, then the items under them. But I was curious, is there a possible shorter method than the one I have going? <br><br>Option Explicit <br><br><br>' From the IniFile class. <br>Private Declare Function GetPrivateProfileSectionNames _ <br>Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" _ <br>(ByVal lpReturnBuffer As String, ByVal nSize As Long, _ <br>ByVal lpName As String) As Long <br><br>' From the IniFile class. <br>Private Declare Function GetPrivateProfileSection _ <br>Lib "kernel32" Alias "GetPrivateProfileSectionA" _ <br>(ByVal lpAppName As String, _ <br>ByVal lpReturnedString As String, ByVal nSize As Long, _ <br>ByVal lpName As String) As Long <br><br>Private Declare Function WritePrivateProfileSection _ <br>Lib "kernel32" Alias "WritePrivateProfileSectionA" _ <br>(ByVal lpAppName As String, ByVal lpString As String, _ <br>ByVal lpName As String) As Long <br><br>' From the IniSection class. <br>Private Declare Function WritePrivateProfileString _ <br>Lib "kernel32" Alias "WritePrivateProfileStringA" _ <br>(ByVal lpApplicationName As String, _ <br>ByVal lpKeyName As Any, ByVal lpString As Any, _ <br>ByVal lpName As String) As Long <br><br><br>Public Sub ReadData() <br>Dim strSections As String <br>Dim lngSize As Long <br>Dim astrSections As Variant <br>Dim astrItems As Variant <br>Dim i As Integer <br>Dim Items As Variant <br><br>On Error GoTo HandleErrors <br>Form1.Label1.Caption = "" <br>' In most cases, 1024 characters is enough, but if it's <br>' not, the code will double that and try again. <br>lngSize = 1024 <br>Do <br>strSections = Space$(lngSize) <br>lngSize = GetPrivateProfileSectionNames(strSections, lngSize, "C:\kbstuff\inistuff\Test.ini" <br>If lngSize = 0 Then <br>' No sections, so get out of here! <br>GoTo ExitHere <br>ElseIf lngSize = Len(strSections) - 2 Then <br>' That's how the API indicates you didn't allow <br>' enough space, but returning the size you originally <br>' specified, less 2. In that case, just double the <br>' buffer, and try again. <br>lngSize = lngSize * 2 <br>Else <br>' Trim the extra stuff. Use lngSize - 1 because <br>' there's an extra vbNullChar at the end of this <br>' string. <br>strSections = Left$(strSections, lngSize - 1) <br>Exit Do <br>End If <br>Loop <br>' Now strSections contains the section names, separated <br>' with vbNullChar. <br>astrSections = Split(strSections, vbNullChar) <br>For i = LBound(astrSections) To UBound(astrSections) - 1 <br>' Add the section to the collection, indicating that <br>' it's not a NEW section. That is, it's not being added <br>' after the file was read. That way, the code there can <br>' know to not bother looking for items when being added <br>' by code later. <br><br>Form1.Label1.Caption = Form1.Label1.Caption & astrSections(i) & vbCrLf <br>astrItems = GetValues(astrSections(i)) <br>For Each Items In astrItems <br>Form1.Label1.Caption = Form1.Label1.Caption & " " & Items & vbCrLf <br>Next <br><br>'Call AddSection(astrSections(i), False) <br>Next i <br><br>ExitHere: <br>Exit Sub <br><br>HandleErrors: <br>Err.Raise Err.Number, Err.Source, Err.Description <br>End Sub <br><br><br>Public Function GetValues(section As Variant) As Variant <br>Dim strSections As String <br>Dim lngSize As Long <br>Dim astrSections As Variant <br>Dim i As Integer <br><br>On Error GoTo HandleErrors <br><br>' In most cases, 1024 characters is enough, but if it's <br>' not, the code will double that and try again. <br>lngSize = 1024 <br>Do <br>strSections = Space$(lngSize) <br>lngSize = GetPrivateProfileSection(section, strSections, lngSize, "C:\kbstuff\inistuff\Test.ini" <br>If lngSize = 0 Then <br>' No sections, so get out of here! <br>GoTo ExitHere <br>ElseIf lngSize = Len(strSections) - 2 Then <br>' That's how the API indicates you didn't allow <br>' enough space, but returning the size you originally <br>' specified, less 2. In that case, just double the <br>' buffer, and try again. <br>lngSize = lngSize * 2 <br>Else <br>' Trim the extra stuff. Use lngSize - 1 because <br>' there's an extra vbNullChar at the end of this <br>' string. <br>strSections = Left$(strSections, lngSize - 1) <br>Exit Do <br>End If <br>Loop <br>' Now strSections contains the section names, separated <br>' with vbNullChar. <br>astrSections = Split(strSections, vbNullChar) <br><br>GetValues = astrSections <br><br>ExitHere: <br>Exit Function <br><br>HandleErrors: <br>Err.Raise Err.Number, Err.Source, Err.Description <br>End Function <br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)