Consulting

Results 1 to 4 of 4

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

  1. #1

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

    Hi Guys,

    I am trying to use ini files which will act as a mini database for outlook. So 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...



    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

  2. #2
    It is not clear what you are trying to put in the listbox, however maybe the following will help. It puts the PreCompletion string in a two column ListBox1 in Userform1 with each item numbered.
    Set the width of column 1 to 4 points less than that of the width of the list box and column 2 to zero width to hide the text and show only the numbers.

    Sub LoadForm()
    'Graham Mayor - https://www.gmayor.com - Last updated - 18 Dec 2018 
    Dim oFrm As UserForm1
    Dim vList1 As Variant
    Dim i As Integer
        Set oFrm = New UserForm1
        vList1 = Split(GetPrivateProfileString32(IniFileName, "Indexes", "PreCompletion"), "|")
        With oFrm
            With .ListBox1
                .ColumnCount = 2
                For i = 0 To UBound(vList1)
                    .AddItem
                    .List(i, 0) = i + 1
                    .List(i, 1) = vList1(i)
                Next i
            End With
            .Show
            'do stuff with ofrm
            Unload oFrm
        End With
        Set oFrm = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks Grayam . It worked fine . I have another question about checking if the users are using right version of code or not. So ini file stores the new version number and within a module I am using below lines to store the current version:
    Const IniFileName As String = "u:\test.ini"
    Const ThisVersion As String = "1.1"
    The below code in the userform will check if the it's the latest version but it gives me error 'ThisVersion' variable not defined although it is present in a module. How can I call the variable stored within a module to a userform??
    Private Sub UserForm_Initialize()
    'Call SaveIni
    Dim m As String
    m = GetPrivateProfileString32("U:\test.ini", "VersionInfo", "Version")
    If m <> ThisVersion Then
    MsgBox GetPrivateProfileString32("U:\test.ini", "VersionInfo", "VersionErrorText")
    Unload Me
    Exit Sub
    End If
    End Sub

  4. #4
    If you want to call getprivateprofilestring from another module then it needs to be a public function, however run the check outside the form
    Sub LoadForm()
    'Graham Mayor - https://www.gmayor.com - Last updated - 18 Dec 2018
    Dim oFrm As UserForm1
    Dim vList1 As Variant
    Dim i As Integer
    Dim m As String
        
        m = GetPrivateProfileString32("U:\test.ini", "VersionInfo", "Version")
        If m <> ThisVersion Then
            MsgBox GetPrivateProfileString32("U:\test.ini", "VersionInfo", "VersionErrorText")
            Exit Sub
        End If
    
        Set oFrm = New UserForm1
        vList1 = Split(GetPrivateProfileString32(IniFileName, "Indexes", "PreCompletion"), "|")
        With oFrm
            With .ListBox1
                .ColumnCount = 2
                For i = 0 To UBound(vList1)
                    .AddItem
                    .List(i, 0) = i + 1
                    .List(i, 1) = vList1(i)
                Next i
            End With
            .Show
            'do stuff with ofrm
        End With
        Unload oFrm
        Set oFrm = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •