Solved

Remember checkbox value vb6

Posted on 2010-11-22
9
836 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

Accepted Solution

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

Author Comment

by:TechLad
ID: 34186934
Which do you think is easyer ?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34186980
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Assisted Solution

by:Frosty555
Frosty555 earned 167 total points
ID: 34187032
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 34195422
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
ID: 34195438
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
ID: 34195442
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
ID: 34844558
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
ID: 34847863
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

730 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