[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
Re: Saving string to .ini
vb Code:
Call WritePrivateProfileString("Section", "TextBox1", txtSave.Text, strFile)
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:
Call WritePrivateProfileString("Section", "TextBox1", CStr(txtSave.Text), strFile)
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
Re: Saving string to .ini
To return a value from an ini I always use.
vb Code:
Private Function GetIni(section As String, key As String, default As String) As Variant
Dim R As String
Dim RetVal As Long
R = String(255, 0)
RetVal = GetPrivateProfileString(section, key, "", R, Len(R), IniFile)
If RetVal = 0 Then
GetIni = default
Else
GetIni = Left(R, InStr(R, Chr(0)) - 1)
End If
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. ;)
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")
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:
Private Sub Form_Load()
Me.Width = GetIni("General", "Width", "5000")
End Sub
Private Function GetIni(section As String, key As String, default As String) As Variant
Dim R As String
Dim RetVal As Long
Dim strFile As String
strFile = App.Path & "\Prefs.ini"
R = String(255, 0)
RetVal = GetPrivateProfileString(section, key, "", R, Len(R), strFile)
If RetVal = 0 Then
GetIni = default
Else
GetIni = Left(R, InStr(R, Chr(0)) - 1)
End If
End Function
If the ini has never been written to before then its a good idea to return a default value. ;)
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.
Re: Saving string to .ini
Yes thats virtually the same as mine. You pass a riDefault value as well. ;)
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
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!
Re: [RESOLVED] Saving string to .ini
vb 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
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:
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. ;)