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)
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.