Solved

Remember checkbox value vb6

Posted on 2010-11-22
9
814 Views
Last Modified: 2012-08-14
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 ?
0
Comment
Question by:TechLad
9 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 167 total points
Comment Utility
you have to store the value somewhere...
some database, text file, registry .... your choice
0
 

Author Comment

by:TechLad
Comment Utility
Which do you think is easyer ?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
 
LVL 31

Assisted Solution

by:Frosty555
Frosty555 earned 167 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

by:Chris Raisin
Comment Utility
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
 
LVL 13

Assisted Solution

by:Chris Raisin
Chris Raisin earned 166 total points
Comment Utility
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
 
LVL 13

Expert Comment

by:Chris Raisin
Comment Utility
You will have to change the names for your check bozes of course (instead of chkCheck1 and chkCheck2)
0
 
LVL 1

Expert Comment

by:bsardeson
Comment Utility
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
 
LVL 13

Expert Comment

by:Chris Raisin
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now