Save And Load Data From INI Files

'Add 1 Text Box and 2 Command Buttons to your Form.
'Run the program, enter text into the Text Box, and press Command1 to save the
'text into the INI File. Press Command2 to get the text from the INI File.
'Insert the following code to your form:

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
'Replace 'C:\MyFile.ini' with the name of your INI File
Const INIFileName = "C:\MyFile.ini"
Private Sub Command1_Click()
'The following code line will save the text in Text1 under the section 'My App Name'
'and Under the Key 'My Key'.

Result = WritePrivateProfileString("My App Name", "My Key", Text1.Text, INIFileName)
End Sub

Private Sub Command2_Click()
Dim MyValue As String * 20
'The following code line will get the value of the key 'My Key' under the
;section 'My App Name', and will insert the vale to the variable 'MyValue'.

Result = GetPrivateProfileString("My App Name", "My Key", "Empty", MyValue, _
Len(MyValue), INIFileName)
MsgBox MyValue
End Sub

Go Back