Results 1 to 39 of 39

Thread: [RESOLVED] Loading values from ini file does nothing

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Loading values from ini file does nothing

    Hi,

    The code below to save the settings for my program.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    4.             (ByVal lpApplicationName As String, _
    5.              ByVal lpKeyName As Any, _
    6.              ByVal lpString As Any, _
    7.              ByVal lpFileName As String) As Long
    8. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    9.             (ByVal lpApplicationName As String, _
    10.              ByVal lpKeyName As Any, _
    11.              ByVal lpDefault As String, _
    12.              ByVal lpReturnedString As String, _
    13.              ByVal nSize As Long, _
    14.              ByVal lpFileName As String) As Long
    15. Public settings As String, lngReturn As Long
    16. Private startup As String
    17.  
    18. Public Sub WriteINI()
    19.   startup = frmMain.chkStartup.Value
    20.   settings = App.Path & "\settings.ini"
    21.   lngReturn = WritePrivateProfileString("Load", "Startup", startup, settings)
    22. End Sub
    23.  
    24. Public Function ReadINI()
    25. Dim strSection As String
    26. Dim strKey As String
    27. Dim strValue As String
    28. Dim strDefault As String
    29. Dim strFile As String
    30. Dim lngLength As Long
    31.    lngReturn = GetPrivateProfileString(strSection, _
    32.                                     strKey, _
    33.                                     strDefault, _
    34.                                     strValue, _
    35.                                     lngLength, _
    36.                                     strFile)
    37.     If FileExists(settings) Then frmMain.chkStartup.Value = lngReturn
    38. End Function

    Saving the data into the files work just not the input although, there is no error the value does not get sent to the check box.

    Thanks,


    Nightwalker
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Loading values from ini file does nothing

    Code:
       
    Dim strSection As String
    Dim strKey As String
    Dim strValue As String
    Dim strDefault As String
    Dim strFile As String
    Dim lngLength As Long
    
    lngReturn = GetPrivateProfileString(strSection, _
                                        strKey, _
                                        strDefault, _
                                        strValue, _
                                        lngLength, _
                                        strFile)
    Of course it won't return anything because you've just declared your Variables and passed them as they are without setting anything to them, so they're empty!!!!!

    strKey = ??? '
    strFile = ??? 'file path
    etc..
    etc..
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    This works for the current data in the file. However, I am doubtful it will work if I expand the data in the file.

    vb Code:
    1. Public Function ReadINI()
    2. Dim strSection As String
    3. Dim strKey As String
    4. Dim strValue As String
    5. Dim strDefault As String
    6. Dim strFile As String
    7. Dim lngLength As Long
    8. Dim ff As Integer
    9. Dim entry
    10.    lngReturn = GetPrivateProfileString(strSection, _
    11.                                     strKey, _
    12.                                     strDefault, _
    13.                                     strValue, _
    14.                                     lngLength, _
    15.                                      strFile)
    16.     ff = FreeFile
    17.      settings = App.Path & "\settings.ini"
    18.     Open settings For Input As #ff
    19.     Do Until EOF(ff)
    20.     Input #ff, entry
    21.     Loop
    22.     Close #ff
    23.     startup = Right(entry, 1)
    24.     If FileExists(settings) Then frmMain.chkStartup.Value = startup
    25. End Function
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Loading values from ini file does nothing

    As pointed out, you need to fill in the values. Plus the lngReturn is not what you're
    after. It provides the length of the buffer returned in the lpReturnedString param.
    Here's an easy to use class for reading/writing config files:

    http://www.vbaccelerator.com/home/vb...ss/article.asp

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    I just found dee-u's example is the FAQ section. I will look into it further tomorrow and see if it is what I am after or not.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    This is what I have used dee-u example above:

    vb Code:
    1. 'declarations for working with Ini files
    2.     Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
    3.         "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
    4.         ByVal nSize As Long, ByVal lpFileName As String) As Long
    5.     Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    6.         "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    7.         ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
    8.         ByVal lpFileName As String) As Long
    9.     Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias _
    10.         "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, _
    11.         ByVal lpFileName As String) As Long
    12.     Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    13.         "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    14.         ByVal lpString As Any, ByVal lpFileName As String) As Long
    15.        Public Settings As String
    16.        Dim cap As String
    17.     '// INI CONTROLLING PROCEDURES
    18.     'reads an Ini string
    19.     Public Function ReadIni(Filename As String, Section As String, Key As String) As String
    20.     Dim RetVal As String * 255, v As Long
    21.       v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
    22.       ReadIni = Left(RetVal, v - 1)
    23.     End Function
    24.     'reads an Ini section
    25.     Public Function ReadIniSection(Filename As String, Section As String) As String
    26.     Dim RetVal As String * 255, v As Long
    27.       v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
    28.        ReadIniSection = Left(RetVal, v - 1)
    29.     End Function
    30.     'writes an Ini string
    31.     Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
    32.       WritePrivateProfileString Section, Key, Value, Filename
    33.     End Sub
    34.     'writes an Ini section
    35.     Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
    36.       WritePrivateProfileSection Section, Value, Filename
    37.     End Sub
    38.    
    39. Public Sub WIni()
    40.   Startup = frmMain.chkStartup.Value
    41.   cap = frmMain.chkStartup.Caption
    42.   Settings = App.Path & "\settings.ini"
    43.   WriteIni Settings, "Load", cap, Startup
    44. End Sub
    45.  
    46. Public Function RIni()
    47.      Dim Str As String
    48.     'read the ini file
    49.       Str = ReadIni(Settings, "Load", cap)
    50.       MsgBox Str
    51. End Function

    In my code I changed:

    vb Code:
    1. Left(RetVal, v - 1)
    to

    vb Code:
    1. Left(RetVal, v)

    because having the "-1" there causes an "Invalid procedure call or argument" error. However, when I run that code nothing appears in the msgbox even though there is data in the ini file.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loading values from ini file does nothing

    If you put 'Option Explicit' at the top of the Declarations Section I think you'll find that the variable 'cap' is not defined. If that is the case, nothing will be returned when you read the file (that's also why you got the "Invalid Procedure Call or Argument" message.)

  8. #8

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Sorry, cap was defined I just had the definition up the top of the module because I thought it might be needed by both WIni and RIni.

    vb Code:
    1. Dim cap As String

    That is in the declarations section of the module in question. It is on line 16 of code in post #6.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loading values from ini file does nothing

    Oops I didn't see that !

    The variables 'cap' and 'Settings' only get set when you write to the ini file with WIni. If you call RIni before WIni both values are Null.

    Perhaps you should set those values in the Form_Load event.

    (BTW you were right to remove the '-1' in the Left function.)

  10. #10

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Quote Originally Posted by Doogle View Post
    Oops I didn't see that !

    The variables 'cap' and 'Settings' only get set when you write to the ini file with WIni. If you call RIni before WIni both values are Null.
    Yeah, although, what I was hoping to do was load the data into the program on load, thus populating the different controls, etc with the values from the ini file. However, as you say if the ini is blank then it will not work. Any ideas on how I could solve this?

    Perhaps you should set those values in the Form_Load event.
    At the moment I have the call to the RIni sub in the load_form event and the call to the WIni sub in the command button click event.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loading values from ini file does nothing

    Well, the variable 'Settings' is going to be constant so it can be defined in the Declarations Secttion as you have done. Allocate it's value in the Form_Load event. If you want to read values for controls from the .ini file then, again, the Form_Load event is probably the place to do it
    Code:
    Public Function RIni(strKey As String) As String
    RIni = ReadIni(Settings, "Load", strKey)
    End Function
    
    
    Private Sub Form_Load()
    Settings = App.Path & "\settings.ini"
    chkstartup.Value = RIni(chkstartup.Caption)
    ' etc

  12. #12

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Unfortunately I receive a type mismatch error on this line:

    vb Code:
    1. chkstartup.Value = RIni(chkstartup.Caption)
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loading values from ini file does nothing

    Strange, it works for me. Does App.Path & "\settings.ini" exist ? If so, does it have a 'Load' section and a Key of whatever chkstartup.Caption is ?

  14. #14

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Quote Originally Posted by Doogle View Post
    Strange, it works for me. Does App.Path & "\settings.ini" exist ? If so, does it have a 'Load' section and a Key of whatever chkstartup.Caption is ?
    Yes, it does exist! Here is some data that is currently in the ini file:

    [Load]
    Load on Startup=1
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  15. #15
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Loading values from ini file does nothing

    I'm sure we all have our different ways of handling INI files. I have one that's pretty lightweight and easy to use. It even lets you pass an INI Section as a parameter, retrieve values with defaults, etc.

    It's a small INI DOM Class named Teeni. Example:
    Code:
    Private Sub Main()
        Dim Settings As Teeni
        Dim Num As Integer
        Dim Key As Teeni
        Dim S As String
        
        Set Settings = New Teeni
        With Settings
            If .Load("my.ini") Then
                With !Window
                    !Top.Value = 10
                    !Left.Value = 120
                    !Width.Value = 500
                    !Height.Value = 320
                End With
                With ![Saved Images]
                    ![1].Value = "abc.gif"
                    ![2].Value = "xyz.gif"
                    Num = 3
                    .Item(Num).Value = "another.gif"
                End With
                .Save
            End If
        End With
        MsgBox "Settings![Saved Images]![2].Value=""" & Settings![Saved Images]![2].Value & """"
        MsgBox "Settings!Window!Top.Value=""" & Settings!Window!Top.Value & """"
        
        'Example using a default Value:
        MsgBox "Settings![Saved Images]![37].Value(""default.gif"")=""" _
             & Settings![Saved Images]![37].Value("default.gif") & """"
        
        For Each Key In Settings![Saved Images]
            S = S & Key.Name & "=""" & Key.Value & """" & vbNewLine
        Next
        MsgBox S
    End Sub
    Attached Files Attached Files

  16. #16

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Although, it appears as if you have hard-coded the values in to the main sub instead of loading them from the ini.

    This is I have in the main sub and it appears to work. However, as can see I have hard-coded the value.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Main()
    4.     Dim Setting As Teeni
    5.     Dim Num As Integer
    6.     Dim Key As Teeni
    7.     Dim S As String
    8.    
    9.     Set Setting = New Teeni
    10.     With Setting
    11.         If .Load("settings.ini") Then
    12.             With ![Load]
    13.                 ![1].Value = "1"
    14.             End With
    15.             .Save
    16.         End If
    17.     End With
    18.    
    19.     'Example using a default Value:
    20.    
    21.     For Each Key In Setting![Load]
    22.         S = S & Key.Name & "=""" & Key.Value & """" & vbNewLine
    23.     Next
    24.     MsgBox S
    25. End Sub

    That works! Although, I am unsure why it works? I manually changed the "Load on Startup=" to "0" to verify this. Also, is it possible to do without having the name of the sections, etc?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  17. #17
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Loading values from ini file does nothing

    The values were being set in Main if the INI file wasn't there.

    I'm not sure what you mean about doing without Section names, INI files are a 2-level deep hierarchy and a Key name must be qualified by its Section. Are you looking for another file format?

    You can have something like:
    Code:
    Dim LoadSection as Teeni
    
    Set LoadSection = Settings![Load]
    
    MsgBox LoadSection![1].Value

  18. #18

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    I mean is there a way to do it without having to hard-code the section names such as [Load], etc but install retrieve that from the ini file itself?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  19. #19
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loading values from ini file does nothing

    I don't think there's a way of doing that using the APIs. The nearest you can get is to read an entire Section, but you still have to pass the section name. The alternative would be to read the .ini file as a flat file and parse the data accordingly. That would complicate the entire process.

    Why do you not want to specify the Section? At some point in time your code is going to have to know what Section it's processing and the key / value pairs in it, so rather than 'discovering' it why not specify it.?

  20. #20

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Quote Originally Posted by Doogle View Post
    Why do you not want to specify the Section? At some point in time your code is going to have to know what Section it's processing and the key / value pairs in it, so rather than 'discovering' it why not specify it.?
    I was thinking that for a big project that having to specify all the sections would be a hassle not to mention having to modify the code where I needed to add a new section or key into the ini file.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  21. #21
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Loading values from ini file does nothing

    Do something like:
    Code:
    Dim Section As Teeni
    Dim Key As Teeni
    Dim S As String
    
    For Each Section In Settings
        For Each Key In Section
            S = S & Section.Name & "." & Key.Name & "=" & Key.Value & vbNewLine
        Next
    Next
    MsgBox S

  22. #22

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    This works!

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Main()
    4.     Dim Settings As Teeni
    5.      Dim Section As Teeni
    6.     Dim Num As Integer
    7.     Dim Key As Teeni
    8.     Dim S As String
    9.    
    10.     Set Settings = New Teeni
    11.     With Settings
    12.         If .Load("settings.ini") Then
    13.     '        With !Window
    14.          '       !Top.Value = 10
    15.           '      !Left.Value = 120
    16.            '     !Width.Value = 500
    17.             '    !Height.Value = 320
    18.             'End With
    19.             'With ![Saved Images]
    20.             '    ![1].Value = "abc.gif"
    21.              '   ![2].Value = "xyz.gif"
    22.               '  Num = 3
    23.                ' .Item(Num).Value = "another.gif"
    24.             'End With
    25.             .Save
    26.         End If
    27.     End With
    28.     'MsgBox "Settings![Saved Images]![2].Value=""" & Settings![Saved Images]![2].Value & """"
    29.     'MsgBox "Settings!Window!Top.Value=""" & Settings!Window!Top.Value & """"
    30.    
    31.     'Example using a default Value:
    32.     'MsgBox "Settings![Saved Images]![37].Value(""default.gif"")=""" _
    33.      '    & Settings![Saved Images]![37].Value("default.gif") & """"
    34.  
    35. For Each Section In Settings
    36.     For Each Key In Section
    37.         S = S & Section.Name & "." & Key.Name & "=" & Key.Value & vbNewLine
    38.     Next
    39. Next
    40. MsgBox S
    41. End Sub

    Edit:

    Well, I seem to encounter the same problem whether I use the above code or the code in post #15 and that is how do I redefine the value of S?

    Example if I do:

    vb Code:
    1. For Each Section In Settings
    2.     For Each Key In Section
    3.         S = Key.Value
    4.         MsgBox S
    5.     Next
    6. Next

    All the values will equal the initial value say 1.
    Last edited by Nightwalker83; Oct 4th, 2011 at 02:40 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  23. #23
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Loading values from ini file does nothing

    Well let's see if this will help...

    I went over the code and I found that when I "cleaned up" a version of Teeni.cls to make the small demo package posted above I had put in a version of the Item() function that does not fit with the rest of the Class. So let's just call it a bug, which should be fixed now.

    The change deals with the difference between passing a String "name" and a non-String (usually Integer or Long) "index" value to the Item() function.


    So for this corrected version let's just load and dump this INI file:
    Code:
    [Section 1]
    Key1=Tom
    Key2=Dick
    Key3=Harry
    [Second Section]
    1=First
    2=Second
    3=Third
    [Last]
    This is it=That's all, folks!
    To do that we'll use:
    Code:
    Private Sub Main()
        Dim Settings As Teeni
        Dim Section As Teeni
        Dim Key As Teeni
        Dim S As Integer
        Dim K As Integer
        
        Set Settings = New Teeni
        Settings.Load "test.ini"
        For Each Section In Settings
            Debug.Print Section.Name
            For Each Key In Section
                Debug.Print Spc(4); Key.Name; "="; Key.Value
            Next
        Next
        Debug.Print
        For S = 1 To Settings.Count
            Debug.Print Settings.Item(S).Name
            For K = 1 To Settings.Item(S).Count
                With Settings.Item(S).Item(K)
                    Debug.Print Spc(4); .Name; "="; .Value
                End With
            Next
        Next
    End Sub
    There we are "dumping" the entire DOM using two different approaches, a For... Each and a For index=1 To Count approach.

    The results I get are:
    Code:
    Section 1
        Key1=Tom
        Key2=Dick
        Key3=Harry
    Second Section
        1=First
        2=Second
        3=Third
    Last
        This is it=That's all, folks!
    
    Section 1
        Key1=Tom
        Key2=Dick
        Key3=Harry
    Second Section
        1=First
        2=Second
        3=Third
    Last
        This is it=That's all, folks!
    Does this help with the problem you're having?
    Attached Files Attached Files

  24. #24

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Unfortunately when I attempt to run the attached project it stops as soon as I do.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  25. #25
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loading values from ini file does nothing

    Quote Originally Posted by dilettante View Post
    So let's just call it a bug
    I always prefer "Unexpected Design Feature"

    @Nightwalker - The code works perfectly for me. I just un-zipped it and ran it as is.

  26. #26

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Loading values from ini file does nothing

    Quote Originally Posted by Doogle View Post
    The code works perfectly for me. I just un-zipped it and ran it as is.
    Yeah, I just realized that but it is a blink and you miss it case.

    Edit:

    Showing the values in a msgbox rather than printing to the debug windows solves that problem.

    This works I just need to add the new controls as I add them.

    vb Code:
    1. Public Function RIni(Settings As String)
    2.      
    3.         Dim config As Teeni
    4.         Dim Section As Teeni
    5.         Dim Key As Teeni
    6.         Dim S As Integer
    7.         Dim K As Integer
    8.        
    9.         Set config = New Teeni
    10.         config.Load "Settings.ini"
    11.         For Each Section In config
    12.             'For Each Key In Section
    13.             'Next
    14.         Next
    15.         For S = 1 To config.Count
    16.             For K = 1 To config.Item(S).Count
    17.                 With config.Item(S).Item(K)
    18.                  If K = 1 Then frmMain.chkStartup.Value = .Value
    19.                  If K = 2 Then frmMain.Check1.Value = .Value
    20.                 End With
    21.             Next
    22.         Next
    23.     End Function
    Last edited by Nightwalker83; Oct 5th, 2011 at 04:34 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  27. #27
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Loading values from ini file does nothing

    So why not:
    Code:
    config.Load "Settings.ini"
    With Config(1) 'Grab 1st Section. Trust the Force, Luke!
        frmMain.chkStartup.Value = ![Whatever I called this].Value(optional default value)
        frmMain.Check1.Value = ![This key's name].Value(optional default value)
    End With

  28. #28

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Loading values from ini file does nothing

    Ah, I will try that.

    Edit:

    This is what I put it does not work, no error, nothing.

    vb Code:
    1. config.Load "Settings.ini"
    2. With config(1) 'Grab 1st Section. Trust the Force, Luke!
    3.     frmMain.chkStartup.Value = ![Load].Value(0)
    4.     frmMain.Check1.Value = ![Load].Value(0)
    5. End With

    I think I will just stick to what I have because it works and less hassle in the long run.
    Last edited by Nightwalker83; Oct 5th, 2011 at 06:11 PM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  29. #29
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Loading values from ini file does nothing

    You called your setting Key "Load" and you want to use the same setting for both CheckBoxes?

  30. #30

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Loading values from ini file does nothing

    oops, no I put the section by mistake. This works.

    vb Code:
    1. Set config = New Teeni
    2.         config.Load "Settings.ini"
    3.         For Each Section In config
    4.                config.Load "Settings.ini"
    5.     With config(1) 'Grab 1st Section. Trust the Force, Luke!
    6.         frmMain.chkStartup.Value = ![Load on startup].Value(0)
    7.         frmMain.Check1.Value = ![Test].Value(0)
    8.     End With
    9.         Next

    Although it defeats the purpose of automatically retrieving the sections, etc such as in post #26.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  31. #31

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Loading values from ini file does nothing

    Why am I receiving error with this code:

    vb Code:
    1. 'Unused for Key nodes.  Marked as Procedure ID = -4!
    2. Public Function NewEnum() As IUnknown
    3.    Set NewEnum = mItems.[_NewEnum]
    4. End Function

    Object variable or with block variable not set when I use:

    vb Code:
    1. Call RIni(Settings)

    I can't figure out why?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  32. #32
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Loading values from ini file does nothing

    What are RIni and Settings?

    The NewEnum only gets used with For... Each.

  33. #33
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Loading values from ini file does nothing

    Also:
    Code:
    Set config = New Teeni
    config.Load "Settings.ini"
    For Each Section In config
        config.Load "Settings.ini"
        With config(1)
            frmMain.chkStartup.Value = ![Load on startup].Value(0)
            frmMain.Check1.Value = ![Test].Value(0)
        End With
    Next
    Is kind of weird. Why try to Load again inside the loop?

    Shouldn't it be:
    Code:
    Set config = New Teeni
    config.Load "Settings.ini"
    With config(1)
        frmMain.chkStartup.Value = ![Load on startup].Value(0)
        frmMain.Check1.Value = ![Test].Value(0)
    End With
    Or even:
    Code:
    Set config = New Teeni
    config.Load "Settings.ini"
    With config(1)
        chkStartup.Value = ![Load on startup].Value(vbUnchecked)
        Check1.Value = ![Test].Value(vbUnchecked)
    End With

  34. #34

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Loading values from ini file does nothing

    Quote Originally Posted by dilettante View Post
    What are RIni and Settings?

    The NewEnum only gets used with For... Each.
    RIni is the function I posted in post #26. If the NewEnum is only suppose to get used with For...Each I think there is another bug in the code? Well, either removing this code completely or commenting out solves the problem.

    vb Code:
    1. For Each Section In config
    2.             'For Each Key In Section
    3.             'Next
    4.         Next

    Quote Originally Posted by dilettante View Post
    Also:
    Code:
    Set config = New Teeni
    config.Load "Settings.ini"
    For Each Section In config
        config.Load "Settings.ini"
        With config(1)
            frmMain.chkStartup.Value = ![Load on startup].Value(0)
            frmMain.Check1.Value = ![Test].Value(0)
        End With
    Next
    Is kind of weird. Why try to Load again inside the loop?

    Shouldn't it be:
    Code:
    Set config = New Teeni
    config.Load "Settings.ini"
    With config(1)
        frmMain.chkStartup.Value = ![Load on startup].Value(0)
        frmMain.Check1.Value = ![Test].Value(0)
    End With
    Or even:
    Code:
    Set config = New Teeni
    config.Load "Settings.ini"
    With config(1)
        chkStartup.Value = ![Load on startup].Value(vbUnchecked)
        Check1.Value = ![Test].Value(vbUnchecked)
    End With
    The code I am using at the moment if not the same as in post #26 is:

    vb Code:
    1. Public Function RIni(Settings As String)
    2.         Dim config As Teeni
    3.         Dim Section As Teeni
    4.         Dim Key As Teeni
    5.         Dim S As Integer
    6.         Dim K As Integer
    7.         Set config = New Teeni
    8.         config.Load "Settings.ini"
    9.         For S = 1 To config.Count
    10.             For K = 1 To config.Item(S).Count
    11.                 With config.Item(S).Item(K)
    12.                  If K = 1 Then frmMain.chkStartup.Value = .Value
    13.                 End With
    14.             Next
    15.         Next
    16.     End Function

    Now that I removed the for each section in config part it works as it is suppose to.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  35. #35
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Loading values from ini file does nothing

    But why are you looping at all?

    Since you know the Section name and Key name, just use:
    Code:
    config.Load "Settings.ini"
    chkStartup.Value = config![sectionname]![keyname].Value
    How would you not know those names?

    And if by some weird chance you did not know the names, first Section, first Key is as easy as:
    Code:
    config.Load "Settings.ini"
    chkStartup.Value = config(1)(1).Value
    Last edited by dilettante; Oct 11th, 2011 at 08:55 PM.

  36. #36

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Loading values from ini file does nothing

    I know what you are saying but I trying to make so there is as little editing to do with outside files as possible.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  37. #37
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Loading values from ini file does nothing

    Well the entire point of using INI files is to create settings based on Section and Key names. Maybe you just want a data file???

  38. #38

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Loading values from ini file does nothing

    Yeah, I will data file instead.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  39. #39
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: [RESOLVED] Loading values from ini file does nothing

    Almost all my programs use INI files now for settings.
    If I don't have multiple sections then its normally just a single section like settings or general.

    This is what I use to read the values:
    In my case I just read everything as a String.

    Code:
    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    
    Public Enum INI_TYPE
        ENBOOL = 3
        ENLONG = 2
        ENINT = 1
        ENSTR = 0
    End Enum
    
    Public Function ReadFromINI(sSection As String, sKey As String, sDefault As String, sFile As String, Optional ByVal pType As INI_TYPE = ENSTR) As Variant
    Dim lret As Long, sValue As String
    On Error GoTo ErrHandler
        If Dir(sFile) <> "" Then
            sValue = Space(1024)
            lret = GetPrivateProfileString(sSection, sKey, sDefault, sValue, Len(sValue) - 1, sFile)
            ReadFromINI = Trim(Left(sValue, lret))
            Select Case pType
                Case 1, 2
                    If IsNumeric(ReadFromINI) = False Or Len(ReadFromINI) = 0 Then
                        ReadFromINI = CInt(sDefault)
                    Else
                        ReadFromINI = CInt(ReadFromINI)
                    End If
                Case 3
                    If IsNumeric(ReadFromINI) = False Or Len(ReadFromINI) = 0 Then
                        ReadFromINI = CBool(sDefault)
                    Else
                        ReadFromINI = CBool(ReadFromINI)
                    End If
                Case Else
                    If Len(ReadFromINI) = 0 Then
                        ReadFromINI = CStr(sDefault)
                    Else
                        ReadFromINI = CStr(ReadFromINI)
                    End If
            End Select
        End If
        Exit Function
    ErrHandler:
        Debug.Print err.Description
        err.Clear
        Resume Next
    End Function
    Example:
    Code:
    [settings]
    title=Demo Program
    quality=3
    fullscreen=1
    posleft=
    Code:
    Private Sub Form_Load()
    
        Dim sFile As String
        sFile = App.Path & "\settings.ini"
        
        '// String test
        Dim sTitle As String
        sTitle = ReadFromINI("settings", "title", "My Program", sFile)
        
        '// Number test
        Dim tmpSpeed As String, iSpeed As Integer
        tmpSpeed = ReadFromINI("settings", "quality", "3", sFile)
        iSpeed = IIf(IsNumeric(tmpSpeed), tmpSpeed, 3)
        
        '// True or False test
        Dim tmpFullscreen As String, bFullscreen As Boolean
        tmpFullscreen = ReadFromINI("settings", "fullscreen", "0", sFile)
        bFullscreen = IIf(IsNumeric(tmpFullscreen), tmpFullscreen, False)
        
        '// Form Position test
        Dim tmpLeft As String
        tmpLeft = ReadFromINI("settings", "posleft", "", sFile)
        If IsNumeric(tmpLeft) Then
            Form1.Left = tmpLeft
        Else
            Form1.Left = (Screen.Width / 2) - (Form1.Width / 2)
        End If
        
        Me.Caption = sTitle
        Debug.Print iSpeed
        If bFullscreen Then Form1.WindowState = 2
    
    End Sub
    Last edited by rory; Oct 13th, 2011 at 03:41 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width