[Tutorial/Snippet][VB6] Reading and writing INI Files
Purpose: Saving settings, etc.
Saving
To save something, write the following under the code for your button or whatever it is your saving from.
Now start to fill this out. Right now that would output the following:
Knowing that, replace section, label, and value with whatever you want to save.
Loading
To load something, put the following code for when you want to load:
or, to assign the value it loaded to a variable,
This works the same way as above and needs to have the same values for section and label or else it will NOT work.
If you need anymore help, just post saying so and I'll do my best to help.
Setting it up
- Create a new module
- Name it something along the lines of "SaveSets"
- Add the following code to your module:
Code:
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long 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 Check As String Public Function Load(Section As String, Key As String) As String Dim lngResult As Long Dim strFileName Dim strResult As String * 300 strFileName = App.Path & "\settings.ini" lngResult = GetPrivateProfileString(Section, Key, strFileName, strResult, Len(strResult), strFileName) Check = App.Path & "\sets.ini" Load = Trim(strResult) End Function Public Function Save(Section As String, Key As String, Content As String) Dim lngResult As Long Dim strFileName strFileName = App.Path & "\settings.ini" lngResult = WritePrivateProfileString(Section, Key, Content, strFileName) End Function
- Now, change where it says "settings.ini" to file name of which you want to read and write.
- All of the rest of the code will stay the same, you shouldn't have much of a reason to chance it.
Using it
Saving
To save something, write the following under the code for your button or whatever it is your saving from.
Code:
Call Save("Section", "Label", "Value")
Code:
[Section] Label = Value
Loading
To load something, put the following code for when you want to load:
Code:
Call Load("Section", "Label")
Code:
MyVariable = Load("Section", "Label")
If you need anymore help, just post saying so and I'll do my best to help.

