[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Remember checkbox value vb6

Posted on 2010-11-22
9
Medium Priority
?
855 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 668 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 668 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 664 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

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!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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