Using Ini files to create text file (with various sections on it ) to store values (1 Viewer)

aman

Registered User.
Local time
Yesterday, 21:40
Joined
Oct 16, 2008
Messages
1,250
Hi Guys,

I am trying to use ini files which will act as a mini database for outlook. So Outlook/VBA code will create a text file (ini file) and data is stored in a text file so everytime you need to change the data then it doesn't require changing the code . The code works fine. The highlighted lines will store data in a listbox eventually and my question is if the listbox is 2 column listbox then how can we change those lines as below:

e.g
1,aaa
2,bbb
3,ccc
4,ddd
5,eee
6,fff
7,ggg

00,aaaa
11,bbbb
22,cccc

etc...



Code:
Const IniFileName As String = "u:\test.ini"
Const ThisVersion As String = "1.1"
' the path and filename to the file containing the information you want to read/write
Private Declare Function GetPrivateProfileStringA Lib "Kernel32" (ByVal strSection As String, _
   ByVal strKey As String, ByVal strDefault As String, ByVal strReturnedString As String, _
   ByVal lngSize As Long, ByVal strFileNameName As String) As Long
  
Private Declare Function WritePrivateProfileStringA Lib "Kernel32" (ByVal strSection As String, _
   ByVal strKey As String, ByVal strString As String, ByVal strFileNameName As String) As Long


Sub SaveIni()


' saves information in the file IniFileName
    If Not WritePrivateProfileString32(IniFileName, "VersionInfo", "Version", "1.0") Then
        MsgBox "Not able to save user info in " & IniFileName, vbExclamation, "Folder does not exist!"
        Exit Sub
    End If
    
    WritePrivateProfileString32 IniFileName, "VersionInfo", "VersionErrorText", "You are not running the correct version of this Software. Please contact Joe Bloggs or Jane Doe"


   WritePrivateProfileString32 IniFileName, "Indexes", "PreCompletion", "aaa|bbb|ccc|ddd|eee|fff|ggg"
   WritePrivateProfileString32 IniFileName, "Indexes", "PostCompletion", "aaaa|bbbb|cccc|dddd|eeee|ffff|gggg"
   WritePrivateProfileString32 IniFileName, "Indexes", "MortgageServices", "111|222|333|444|555|666|777|888|999|000"


End Sub
Private Function WritePrivateProfileString32(ByVal strFileName As String, ByVal strSection As String, ByVal strKey As String, ByVal strValue As String) As Boolean
Dim lngValid As Long
    On Error Resume Next
    lngValid = WritePrivateProfileStringA(strSection, strKey, strValue, strFileName)
    If lngValid > 0 Then WritePrivateProfileString32 = True
    On Error GoTo 0
End Function


Private Function GetPrivateProfileString32(ByVal strFileName As String, ByVal strSection As String, ByVal strKey As String, Optional strDefault) As String
Dim strReturnString As String, lngSize As Long, lngValid As Long
    On Error Resume Next
    If IsMissing(strDefault) Then strDefault = ""
    strReturnString = Space(2048)
    lngSize = Len(strReturnString)
    lngValid = GetPrivateProfileStringA(strSection, strKey, strDefault, strReturnString, lngSize, strFileName)
    GetPrivateProfileString32 = Left(strReturnString, lngValid)
'   On Error GoTo 0
End Function
 

arnelgp

error reading drive A:
Local time
Today, 12:40
Joined
May 7, 2009
Messages
9,616
first you must save your list items in ini file delimited with comma and pipe:
Code:
...
   WritePrivateProfileString32 IniFileName, "Indexes", "PreCompletion", "1,aaa|2,bbb|3,ccc|4,ddd|5,eee|6,fff|7,ggg"
create a public function that will parse this data:
Code:
Public Function fnFillList(l As ListBox)
Dim m As String
Dim vList As Variant
Dim v As Variant
m = GetPrivateProfileString32("E:\test.ini", "Indexes", "PreCompletion")
vList = Split(m, "|")
For Each v In vList
    l.AddItem v
Next
End Function
create a Listbox in a Form.
on it's property->data->row source type: value list
on it's property->format->column count: 2

on the form's Load Event, fill the listbox:
Code:
Private Sub Form_Load()
Call fnFillList(Me.List2)
End Sub
 

aman

Registered User.
Local time
Yesterday, 21:40
Joined
Oct 16, 2008
Messages
1,250
Thanks Arnelp, I tried it but nothing got populated in the listbox.

When I step through the code then I can see the values get stored in variable 'v' but nothing shows in the listbox :
Code:
Public Function fnFillList(l As ListBox)
Dim m As String
Dim vList As Variant
Dim v As Variant
m = GetPrivateProfileString32("U:\test.ini", "Indexes", "PreCompletion")
vList = Split(m, "|")
For Each v In vList
    l.AddItem v
Next
End Function
 

arnelgp

error reading drive A:
Local time
Today, 12:40
Joined
May 7, 2009
Messages
9,616
again plz review the steps. i already tested it and it worked for me.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom