Results 1 to 22 of 22

Thread: Saving settings in VB programs.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    5

    Saving settings in VB programs.

    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.

  2. #2
    Lively Member trip85's Avatar
    Join Date
    Jun 2000
    Location
    Chalfont, PA
    Posts
    106

    Re: Saving settings in VB programs.

    You can use the Windows registry. Look into SaveSetting and GetSetting in the MSDN dcumentation.
    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

  3. #3
    Lively Member trip85's Avatar
    Join Date
    Jun 2000
    Location
    Chalfont, PA
    Posts
    106

    Re: Saving settings in VB programs.

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    5

    Re: Saving settings in VB programs.

    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.

  5. #5
    Fanatic Member vbPoet's Avatar
    Join Date
    Feb 2005
    Location
    Searching ..
    Posts
    669

    Re: Saving settings in VB programs.

    Xml could be better path..

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving settings in VB programs.

    I just use a few text files in the app.path. They aren't .txt, but that's so others can't open them. It's much easier than using a database.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397

    Re: Saving settings in VB programs.

    You might find these functions useful if you want to use a ini file to save settings.

    I got it somewhere on these forums but can't remember who wrote it.
    Attached Files Attached Files

  8. #8

  9. #9
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Saving settings in VB programs.

    Quote Originally Posted by Narfy
    You might find these functions useful if you want to use a ini file to save settings.

    I got it somewhere on these forums but can't remember who wrote it.
    Cheers Narfy, I've been looking for this for a while
    thanks.
    Zeegnahtuer?

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving settings in VB programs.

    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:
    1. Private Sub GetRecentFiles()
    2. ' This procedure makes use of the GetAllSettings function,
    3. ' which returns an array of values from the Windows registry. In this
    4. ' case, the registry contains the files most recently opened.
    5.     On Error Resume Next
    6.     Dim i As Integer
    7.     Dim varFiles As Variant ' Varible to store the returned array.
    8.     ' Get Recently Opened Files from the registry using the GetAllSettings statement.
    9.     If GetSetting("MRUList", "MRUFileKey", "RecentFile1") = Empty Then Exit Sub
    10.    
    11.     varFiles = GetAllSettings("MRUList", "MRUFileKey")
    12.    
    13.     mnuSep1.Visible = True
    14.     For i = 0 To UBound(varFiles, 1)
    15.         mnuFileMRU(i).Visible = True
    16.         mnuFileMRU(i + 1).Caption = varFiles(i, 1)
    17.         mnuFileMRU(i + 1).Visible = True
    18.     Next
    19. End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    5

    Re: Saving settings in VB programs.

    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!

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving settings in VB programs.

    You are absolutlely on the right track Tigerman. Here is an example of what I think you need. Modify as necessary
    VB Code:
    1. Private Sub mnuDefault_Click()
    2. SaveSetting "NameOfYourApp", "AppropriateSectionName", "AppropriateKeyName", Text1.Text"
    3. End Sub
    4.  
    5. Private Sub Form_Load()
    6. Dim strMySetting As String
    7. strMySetting = GetSetting "NameOfYourApp", "AppropriateSectionName", "AppropriateKeyName"
    8. Text1.Text = strMySetting
    9. End Sub
    Clear as mud?

  13. #13
    Member
    Join Date
    Feb 2005
    Location
    Chennai-India
    Posts
    38

    Re: Saving settings in VB programs.

    I hope using .ini files is also good method.
    -J S VijayRagavan

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving settings in VB programs.

    Quote Originally Posted by ragavijay_1979
    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.

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Saving settings in VB programs.

    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.

  16. #16
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Saving settings in VB programs.

    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 ini file, 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.

  17. #17
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving settings in VB programs.

    Quote Originally Posted by Keithuk
    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 ini file, 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.

  18. #18

  19. #19
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Saving settings in VB programs.

    Quote Originally Posted by MartinLiss
    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)

    2 idiots don't make a genius.

  20. #20

  21. #21
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Saving settings in VB programs.

    Quote Originally Posted by MartinLiss
    Can you use such things as WritePrivateProfileString with entries longer than 256?
    Possibly, but my point is that this is a limitation of the Win32 API, not of INI Files.
    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)

    2 idiots don't make a genius.

  22. #22
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Unhappy Re: Saving settings in VB programs.

    Quote Originally Posted by Hack
    You are absolutlely on the right track Tigerman. Here is an example of what I think you need. Modify as necessary
    VB Code:
    1. Private Sub mnuDefault_Click()
    2. SaveSetting "NameOfYourApp", "AppropriateSectionName", "AppropriateKeyName", Text1.Text"
    3. End Sub
    4.  
    5. Private Sub Form_Load()
    6. Dim strMySetting As String
    7. strMySetting = GetSetting "NameOfYourApp", "AppropriateSectionName", "AppropriateKeyName"
    8. Text1.Text = strMySetting
    9. End Sub
    Clear as mud?
    I don't get the part of "NameOfYourApp", "AppropriateSectionName", "AppropriateKeyName", what are each of them ?

    thnx in advance,
    Phil.D.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width