Can someone give me some advice on coding a VB program so that values can be saved, and reappear when the program is reopened? This would also apply to coding a program so that you can set default values so the user can customize the application.
You can use the Windows registry. Look into SaveSetting and GetSetting in the MSDN dcumentation.
GetSetting Function
Returns a key setting value from an application's entry in the Windowsregistry.
Syntax
GetSetting(appname, section, key[, default])
The GetSetting function syntax has thesenamed arguments:
Part Description
appname Required.String expression containing the name of the application or project whose key setting is requested.
section Required. String expression containing the name of the section where the key setting is found.
key Required. String expression containing the name of the key setting to return.
default Optional.Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").
Remarks
If any of the items named in the GetSetting arguments do not exist, GetSetting returns the value of default.
SaveSetting Statement
Saves or creates an application entry in the application's entry in the Windowsregistry.
Syntax
SaveSetting appname, section, key, setting
The SaveSetting statement syntax has thesenamed arguments:
Part Description
appname Required.String expression containing the name of the application orproject to which the setting applies.
section Required. String expression containing the name of the section where the key setting is being saved.
key Required. String expression containing the name of the key setting being saved.
setting Required.Expression containing the value that key is being set to.
Remarks
An error occurs if the key setting can’t be saved for any reason
Thanks in Advance!
TRIP85
In the game of life, it's not whether you win or lose, it's how drunk you get.
-Homer Simpson
Thanks, Trip85. I did try the GetSetting and SaveSetting once and I couldn't get it to work. I'll give it another look over and see if I can find where I made the mistake.
Another useful registry function that goes with SaveSetting and GetSetting is GetAllSettings. This will return everything that you have saved under a specifc key. You can read these settings into an array, iterate through the array, and do whatever you want to do with the stored registry settings. This is an example of a sub I use when imploying an MRU in an application.
VB Code:
Private Sub GetRecentFiles()
' This procedure makes use of the GetAllSettings function,
' which returns an array of values from the Windows registry. In this
' case, the registry contains the files most recently opened.
On Error Resume Next
Dim i As Integer
Dim varFiles As Variant ' Varible to store the returned array.
' Get Recently Opened Files from the registry using the GetAllSettings statement.
If GetSetting("MRUList", "MRUFileKey", "RecentFile1") = Empty Then Exit Sub
Thanks to all for your help. Since I am somewhat new to VB, some of the answers are hard to decifer. I'm having a hard time figuring out exactly what I need to code. Here is exactly what I need to happen:
1. User types text in text1.
2. User chooses a menu option called "Change Default" to save the text typed in text1.
3. When user reopens the program, the text they typed in text1 appears in text1.
Logic tells me I need to add "SaveSetting" coding in Private Sub mnuDefault_Click() (the name of the "Change Default" menu option), then add "GetSetting" in Private Sub Form_Load()?
Am I on track and what is the exact code I should write? Sorry if my ignorace about VB seems annoying. Thanks, Marty for the welcome. I can see from my first thread that this forum is filled with good people wanting to help. Hopefully at some point I will gain enough knowledge to lend a hand myself. Thank you all!
I hope using .ini files is also good method.
-J S VijayRagavan
They are fine, but as they are nothing more than physical text files residing on a user's computer, they can easily be deleted or modifed using Notepad. The registry is a bit more secure, but if security really isn't an issue, then .ini will certainly work.
People can open ini files with their editors, usually. I like to name the text file something that an editor won't open. That usually keeps them out of there.
Don't use SaveSetting and GetSetting that just puts c*** in the registry. If you uninstall your app then this c*** is left behind. Use an inifile, if your app is setup correctly then it can check to see if the values received from the ini are valid. And you don't have to use Yourapp.ini it can be Yourapp.dll.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Don't use SaveSetting and GetSetting that just puts c*** in the registry. If you uninstall your app then this c*** is left behind. Use an inifile, if your app is setup correctly then it can check to see if the values received from the ini are valid. And you don't have to use Yourapp.ini it can be Yourapp.dll.
Actually, I've gotten away from using either the .ini file (which I haven't used in years) or the registry. I include a CUSTOM_SETTINGS db table with my application and save all of those things that would be a registry setting or an .ini setting in a table.
Yes, it is interesting. The author provides some examples of "flaws inherent to INI files". This is a mis-speak, those are limitations of the Win32 API, not INI files themselves. Text files are not limited to 64Kb, and you can make entries as long as you want.
Nobody knows what software they want until after you've delivered what they originally asked for.
Don't solve problems which don't exist.
"If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)