Remember checkbox value vb6

Hi,

Is it possible to write some sort of code that will Remember checkbox value in vb6 form when the program has been close ? So that when the program is restarted it remembers what value it was set to ?
TechLadAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
you have to store the value somewhere...
some database, text file, registry .... your choice
0
 
TechLadAuthor Commented:
Which do you think is easyer ?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
it's more about the requirement than about being easier...

if the setting has to be remembered only per PC, the registry is just fine. read Read/Write Registry VB to find plenty of example on the internet

if the setting is per application globally, you could put the text file, BUT as the config file will need to be central, all users would need to update that file ...

to have a database, on the other side, is not necessarily something required for the application in general, but usually the case, and not difficult at all ...

adodb connection + command to run some sql to select/update the "setting" in a table ...

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Frosty555Connect With a Mentor Commented:
Use SaveSetting() and LoadSetting(), this is VB6's built in method for saving and remembering settings. Behind the scenes it stores it in a folder in the registry reserved for VB application settings.

http://msdn.microsoft.com/en-us/library/3kz7fyks(VS.80).aspx
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
The Registry is the best, using GetSetting() and SaveSetting() as follows:

      'Save settings of CheckBoxes 1 and 2
      'Place these settings in your Form_Unload() subroutine
      SaveSetting App.Exename,,"Setup","chkCheck1.Value", chkCheck1.value
      SaveSetting App.Exename,,"Setup","chkCheck2.Value", chkCheck2.value

      'Restore Settings of CheckBoxes 1 and 2    
      'Retrieve these settings in your Form_Load() subroutine
      '(if no setting is found in registry, the default is vbUnchecked)
      chkBox1.Value = GetSetting _
                                 (App.ExeName,"Setup","chkCheck1.Value", vbUnchecked)
      chkBox2.Value = GetSetting _
                                 (App.ExeName,"Setup","chkCheck2.Value", vbUnchecked)

0
 
Chris RaisinConnect With a Mentor (Retired Analyst/Programmer)Commented:
oops...too many commas...

Retyped it again below.
'Save settings of CheckBoxes 1 and 2
'Place these settings in your Form_Unload() subroutine
 SaveSetting App.Exename, "Setup","chkCheck1.Value", chkCheck1.value
 SaveSetting App.Exename, "Setup","chkCheck2.Value", chkCheck2.value

'Restore Settings of CheckBoxes 1 and 2    
'Retrieve these settings in your Form_Load() subroutine
'(if no setting is found in registry, the default is vbUnchecked)
 chkBox1.Value = GetSetting _
                 (App.ExeName,"Setup","chkCheck1.Value", vbUnchecked)
 chkBox2.Value = GetSetting _
                 (App.ExeName,"Setup","chkCheck2.Value", vbUnchecked)

Open in new window

0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
You will have to change the names for your check bozes of course (instead of chkCheck1 and chkCheck2)
0
 
bsardesonCommented:
To meet needs for temporary or permanent localization or network disconnections I prefer .ini files (sample code included) or XML files due to portability, transferability and their database friendly nature.

Once an INI or XML is written locally, one can perform any number of operations with the file.  Most often, the updating of a centralized database if a network looses connectivity.
Option Explicit

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function IniFileName() As String
  IniFileName = "c:\[yourpath here]\settings.ini"
End Function


Private Function ReadINI(ByVal Section As String, ByVal Keyname As String) As String
Dim Worked As Long
Dim RetStr As String * 128
Dim StrSize As Long
Dim dCharCount As Double
Dim sKeyValue As String

  dCharCount = 0
  sKeyValue = ""
  If Section = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
  Else
    RetStr = Space(128)
    StrSize = Len(RetStr)
    Worked = GetPrivateProfileString(Section, Keyname, "", RetStr, StrSize, IniFileName)
    If Worked Then
      dCharCount = Worked
      sKeyValue = Left$(RetStr, Worked)
    End If
  End If
  ReadINI = sKeyValue
End Function

Private Function WriteINI(ByVal Section As String, ByVal Keyname As String, ByVal Wstr As String) As String
Dim Worked As Long
Dim dCharCount As Double
Dim sKeyValue As String

  dCharCount = 0
  sKeyValue = ""
  If Section = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
  Else
    Worked = WritePrivateProfileString(Section, Keyname, Wstr, IniFileName)
    If Worked Then
      dCharCount = Worked
      sKeyValue = Wstr
    End If
    WriteINI = sKeyValue
  End If
End Function

Open in new window

0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
True, that is a good option and is especially a good method if you are distributing standard settings across many machines.

It is best to have seperate files in this case, one for users "environment" preferences (such as screen nackground color, position of boxes etc) and one for "default" program settings (such as default values in list boxes, default settings for check boxes etc.)

That way, "across the board" changes can be made on many machines simply by sending a fresh ".INI" or ".XML" file just for the programs's default settings without upsetting the local user's settings in the process. Simply send the new file (perhaps by email) to the users and ask them to copy them in to the program's apllication folder (or, better still,create an easy "install" process which does this for them and get them to run it).

On further thought, that is probably the best way to go if your application runs on multiple machines in different loactions, rather than using "setsetting" and "savesetting".

You will need to write the code to read and write to the XML or INI files of course.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.