Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EASY Method: write/read values to .ini file

Windows API

EASY Method: write/read values to .ini file

by  billybobk  Posted    (Edited  )
Use .ini files to store and retrieve values. Just copy and paste!
If you want to store values for quick store/change and retrieval between when your program closes and when it re-opens, then use an .ini file, and keep it right there in the same folder as your VB .exe. Let's say that Red was the last value you had in a textbox, and you have to close the program, but you want Red to be in the textbox when you re-open the program. Or a label's caption. Use these Windows API's to do the trick!

Add a code module to your project (Project menu) and paste in this code:
******************************
Public Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName 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 Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpValue As String, _
ByVal lpFilename As String) As Long

Public Sub ProfileSaveItem(lpSectionName As String, _
lpKeyName As String, _
lpValue As String, _
lpFilename As String)

Call WritePrivateProfileString(lpSectionName, _
lpKeyName, _
lpValue, _
lpFilename)

End Sub
********************************************************
Things to remember:
1. in the GET function, you enter the Section Name (lpSectionName), the Key Name (lpKeyName) and the File Name (lpFileName), and it returns to you the lpReturnedString (the VALUE associated with the key name). So the FileName is myIniFile.ini, the section name is [mySection], and the keyname is myKey=myValue... You give it the parameters and myValue will be returned to you. Assign it to a variable or a textbox and Bingo! Like this: txtBox.text = lpReturnedString
2. In the WRITE function, you enter the Section Name (lpSectionName), the Key Name (lpKeyName) and the File Name (lpFileName) AND THE VALUE, and it writes whatever value you give next to the keyname.
3. In the WRITE function, if the section name does not already exist, IT IS CREATED on the fly. If the Key Name does not already exist, IT IS CREATED on the fly. And of course so is the value. Only the .ini file (lpFileName) must already exist.
4. Make sure you set the value of nSize large enough, since it is a buffer that holds your string. If nSize is set too small, your string will be truncated to that length.
**********************************************
How about an example: I create a blank ini file in Notepad and save it as myInifile.ini. (lpFilename) I have to close the program now, but when I re-open the program later, I want to remember what was in the textbox (I closed the app and went away for 3 weeks in Tahiti. I guaran-freakin-tee you I forgot.) On the form is a "Close" button called cmdEnd. A textbox called txtBox. The text you typed in is "Red Crayons". The IniFile is called myInifile.ini. SAVE THE INIFILE IN THE SAME FOLDER AS THE APP FOR "App.Path" TO WORK! Do this:
***********************************************
Option Explicit
Dim lpSectionName As String
Dim lpKeyName As String
Dim lpValue As String
Dim lpFilename As String
Dim lpReturnedString As String
Dim nSize As Long

Private Sub cmdEnd_Click()

lpFileName = app.path & "\myInifile.ini"

lpFilename)

lpSectionName = "Crayolas"
lpKeyName = "Color1"
lpValue = txtBox.text

Call ProfileSaveItem(lpSectionName, _
lpKeyName, _
lpValue, _

End
End Sub
***********************************
Now open the ini file. You will see this:

[Crayolas]
Color1=Red Crayons
************************************
Now I'm back from Tahiti. I open the program and because I have the following code:

Private Sub Form_Load()

lpFileName = app.path & "\myInifile.ini"

lpReturnedString = Space$(255)
nSize = Len(lpReturnedString)
nSize = GetPrivateProfileString("Crayolas", "Color1", _
" ", lpReturnedString, 50, lpFilename)
lpReturnedString = Mid(lpReturnedString, 1, nSize)
txtBox.text = lpReturnedString
End Sub

The words "Red Crayon" are right there in the textbox!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top