Results 1 to 12 of 12

Thread: [RESOLVED] Saving string to .ini

Hybrid View

  1. #1

    Thread Starter
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Resolved [RESOLVED] Saving string to .ini

    I can get it to work with integers, but when I try coding my program to save a string, it doesn't seem to work. I think I am just formatting the code incorrectly.

    Here is my code in my module:
    Code:
    Declare Function WritePrivateProfileString Lib "kernel32" _
               Alias "WritePrivateProfileStringA" _
                     (ByVal sSectionName As String, _
                      ByVal sKeyName As String, _
                      ByVal sString As String, _
                      ByVal sFileName As String) As Long
    
    Declare Function GetPrivateProfileString Lib "kernel32" _
               Alias "GetPrivateProfileStringA" _
                     (ByVal sSectionName As String, _
                      ByVal sKeyName As String, _
                      ByVal lDefault As Long, _
                      ByVal sFileName As String) As Long
    And here is my code in the form:

    Code:
    Option Explicit
    
    Public strFile As String
    
    
    Private Sub cmdsave_Click()
    strFile = App.Path & "\Prefs.ini"
    
    WritePrivateProfileString = ("Section","TextBox1",txtSave.Text,strFile)
    
    End Sub
    This code is simplified to make it simpler, but there is something wrong with that last line. How do I get it to save txtSave.Text in the Prefs.ini?
    Also, how would I load it into another text box or string or something?
    Thanks in advance

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Saving string to .ini

    vb Code:
    1. Call WritePrivateProfileString("Section", "TextBox1", txtSave.Text, strFile)

  3. #3
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Saving string to .ini

    The variables in WritePrivateProfileString are all Strings. Therefore if the text is a number then it won't work. You have to convert the text in txtSave to a String.
    vb Code:
    1. Call WritePrivateProfileString("Section", "TextBox1", CStr(txtSave.Text), strFile)
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  4. #4

    Thread Starter
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Re: Saving string to .ini

    Thanks guys, that helps. I ended up doing something a little different, and saving the string to the INI works now. I am having problems bringing that string back into the program though (kinda an important aspect, lol). Anyways, here is what I have in my form:

    Code:
    Option Explicit
    
    Public strFile As String
    Public strText As String
    Public lReturn As String
    Public lReturn2 As String
    
    
    
    Private Sub cmdload_Click()
    strFile = App.Path & "\Prefs.ini"
    strText = txtLoad.Text
    
    lReturn2 = GetPrivateProfileString("Section", "TextBox1", "default", strFile)
    MsgBox (lReturn2)
    End Sub
    
    Private Sub cmdsave_Click()
    strFile = App.Path & "\Prefs.ini"
    strText = txtSave.Text
    
    lReturn = WritePrivateProfileString("Section", "TextBox1", strText, strFile)
    
    End Sub
    Do you guys see something wrong with the GetPrivateProfileString that I am missing?

    Thanks again in advance.

    Mat

  5. #5
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Saving string to .ini

    To return a value from an ini I always use.
    vb Code:
    1. Private Function GetIni(section As String, key As String, default As String) As Variant
    2.  
    3. Dim R As String
    4. Dim RetVal As Long
    5.  
    6. R = String(255, 0)
    7. RetVal = GetPrivateProfileString(section, key, "", R, Len(R), IniFile)
    8. If RetVal = 0 Then
    9.     GetIni = default
    10. Else
    11.     GetIni = Left(R, InStr(R, Chr(0)) - 1)
    12. End If
    13.  
    14. End Function
    The default is a value you are expecting.

    Code:
    lReturn = WritePrivateProfileString("Section", "TextBox1", strText, strFile)
    MsgBox (lReturn2)
    lReturn2 will only return a Long value you have to convert this to a String with Left(R, InStr(R, Chr(0)) - 1). If the ini hasn't been used before then you will need the default value.

    Its always a Variant because an ini can return a Integer, Long, String, Double, Currency or what ever.
    Last edited by Keithuk; Apr 13th, 2007 at 07:11 PM.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  6. #6

    Thread Starter
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Re: Saving string to .ini

    What order would I put that code in then to make it work. Could I put your Private Function in and then with cmdLoad's click sub just put:
    Code:
    GetIni ("Section", "Textbox1", "default")

  7. #7
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Saving string to .ini

    You can put GetIni as Private in a Form or Public in a Module. It depends how many Forms need to read the ini file.

    If you have some idea what value should be in the txtSave.Text then you can send this value to be read from the ini file
    e.g. If you want the width of a Form on startup then
    vb Code:
    1. Private Sub Form_Load()
    2.  
    3. Me.Width = GetIni("General", "Width", "5000")
    4.  
    5. End Sub
    6. Private Function GetIni(section As String, key As String, default As String) As Variant
    7.  
    8. Dim R As String
    9. Dim RetVal As Long
    10. Dim strFile As String
    11.  
    12. strFile = App.Path & "\Prefs.ini"
    13. R = String(255, 0)
    14. RetVal = GetPrivateProfileString(section, key, "", R, Len(R), strFile)
    15. If RetVal = 0 Then
    16.     GetIni = default
    17. Else    
    18.     GetIni = Left(R, InStr(R, Chr(0)) - 1)
    19. End If
    20.  
    21. End Function
    If the ini has never been written to before then its a good idea to return a default value.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  8. #8
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Saving string to .ini

    Code:
    Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA" (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 WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Public Sub WriteINI(wiSection As String, wiKey As String, wiValue As String, wiFile As String)
        WritePrivateProfileString wiSection, wiKey, wiValue, App.Path & "\" & wiFile
    End Sub
    Public Function ReadINI(riSection As String, riKey As String, riFile As String, riDefault As String)
        Dim sRiBuffer As String
        Dim sRiValue As String
        Dim sRiLong As String
        Dim INIFile As String
        INIFile = App.Path & "\" & riFile
        If Dir(INIFile) <> "" Then
            sRiBuffer = String(255, vbNull)
            sRiLong = GetPrivateProfileString(riSection, riKey, Chr(1), sRiBuffer, 255, INIFile)
            If Left$(sRiBuffer, 1) <> Chr(1) Then
                sRiValue = Left$(sRiBuffer, sRiLong)
                If sRiValue <> "" Then
                    ReadINI = sRiValue
                Else
                    ReadINI = riDefault
                End If
            Else
                ReadINI = riDefault
            End If
        Else
            ReadINI = riDefault
        End If
    End Function
    This is my Ini Read/Write functions i use.

  9. #9
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Saving string to .ini

    Yes thats virtually the same as mine. You pass a riDefault value as well.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  10. #10

    Thread Starter
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Re: Saving string to .ini

    Ok, thanks a lot. I got it to work for strings, but now when I try to write an integer to the ini, it gets an error and vb shuts down. Any ideas on why this is happening, because it seems like it should be able to write any type of variable to it.
    Thanks,
    Matt

  11. #11

    Thread Starter
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Re: Saving string to .ini

    nevermind, I think I got it. I just converted the integer to a string and did it that way. I'm sure there are better ways to do it, but this works for me. Thanks a lot guys!

  12. #12
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: [RESOLVED] Saving string to .ini

    vb Code:
    1. Declare Function WritePrivateProfileString Lib "kernel32" _
    2. Alias "WritePrivateProfileStringA" (ByVal sSectionName As String, _
    3. ByVal sKeyName As String, ByVal sString As String, _
    4. ByVal sFileName As String) As Long
    Quote Originally Posted by Keithuk
    The variables in WritePrivateProfileString are all Strings. Therefore if the text is a number then it won't work. You have to convert the text in txtSave to a String.
    vb Code:
    1. Call WritePrivateProfileString("Section", "TextBox1", CStr(txtSave.Text), strFile)
    As I've said before you have to convert everything to a String, look at the values in the API call they are ALL Strings. So use CStr.
    Last edited by Keithuk; Apr 16th, 2007 at 06:49 AM.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

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