Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

Windows API

EASY Method: write/read values to .ini file by billybobk
Posted: 30 Jul 03

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, _

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"

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

Call ProfileSaveItem(lpSectionName, _
                     lpKeyName, _
                     lpValue, _
End Sub
Now open the ini file. You will see this:

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!

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close