# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB - Using SaveSetting and GetSetting to store and retrieve data from the Registry

## MartinLiss

Here is how to use the Registry


' Save text1's value
SaveSetting App.EXEName, "textboxes", "text1", text1.Text

' Get text1's value
text1.text = GetSetting (App.EXEName, "textboxes", "text1", "")

SaveSetting stores the data in the Registry (in HKEY_CURRENT_USER|Software|VB and VBA Program Settings|YourAppName). The four parts of the function are the name under which it is stored (App.EXEName in this case), "textboxes" in this example is like the section name in an ini file, "text1" is like the key in a line of data in the ini file, and text1.text is the value.

GetSetting returns the value. The 4th parameter ("" in this case) is optional and it is the default if no registry entry is found.

----------


## crptcblade

You can also go deeper than one level if you feel you need to for organizational purposes.  Just use a backslash ("\") to separate the key names...

VB Code:
Call SaveSetting(App.EXEName, "Textboxes\frmMain", "Text1", frmMain.Text1.Text)

will create a key under "HKEY_CURRENT_USER\Software\VB and VBA Program Settings\YourAppName\Textboxes\frmMain

 :Smilie:

----------


## MartinLiss

I didn't know that. Thanks.

----------


## Romano

Got the gist of both now and thanks very much for your time.


bye

----------


## shmuclez

how would you do that with a list?

----------


## MartinLiss

It depends on the size of the list. If it's a reasonably small list you could combine the data in a string separated by some unusual character and store it in one registry entry. Otherwise you should use a database or textfile to store the data.

----------


## fbtucker

Have you ever tried saving to a registry in a Windows CE device.... i'm actually usine Embedded CE 3.0 which is very similar to vb6, but there are some differences.....i tried your code but it didn't recognize savesettings must be something similar with a ce device

----------


## MartinLiss

I don't have any experience with embedded VB but I did a Google search for _getsetting embedded_ and I came up with this article.

----------


## fbtucker

> _Originally posted by MartinLiss_ 
> *Here is how to use the Registry
> 
> 
> ' Save text1's value
> SaveSetting App.EXEName, "textboxes", "text1", text1.Text
> 
> ' Get text1's value
> text1.text = GetSetting App.EXEName, "textboxes", "text1", "")
> ...



<B>Where is you text1.text file located????</B>

----------


## MartinLiss

text1.text isn't a file, it's the value of a textbox.

----------


## cocoy

can you store the data in registry only in (HKEY_CURRENT_USER|YourAppName)???? how?

----------


## MartinLiss

Search this forum for _registry_ and you'll find a lot of examples like this one.

----------


## kayos

very, very useful information.

----------


## Conquest

Hy, I'm new here but I understand in VB very good,
can You tell me how to save text from MSFlexGrid!

----------


## MartinLiss

Conquest. Please see this thread.

----------


## draven2kg

How to save for checkboxes?

----------


## MartinLiss

Very similarly



VB Code:
SaveSetting App.EXEName, "checkboxes or anything you want", "MyCheckbox", text1.Text
 ' Here I have the default for the checkbox set to be unchecked. You may
' want to change that to vbChecked or even vbGrayed
Check1.Value = GetSetting (App.EXEName, "checkboxes or anything you want", "MyCheckbox", vbUnchecked)

----------


## draven2kg

I am trying to get the value of a checkbox that i have compiled in an exe. 



This Function is in an activex component. It calls the form and checks the value that should be by default turned on in my exe. However, i seem to be having some trouble getting the value. Reading this post i thought the code below will give me the value of either 1 or 0 but that doesnt seem to be the case. 

Public Function GetValue()

Dim AValue

AValue = GetSetting("TestSSvr", "checkboxes\frmdvan", "chkA", "")

GetValue = AValue

End Function

' I dont seem to be getting anything here
'Using this code AValue = GetSetting("TestSSvr", "checkboxes\frmdvan", "chkA", vbUnchecked) I get a 0 all the time????


'==========Here is the code that saves the value in my exe to the registry 'of the checkbox.This code works in loading the value from the registry and saving to it. But i dont seem to be able to get the value from my active x component when i call it(^Above)



Private Sub Form_Load()

chkA.Value = GetSetting(App.EXEName, "checkboxes\frmdvan", "chkA", "")


End Sub




Private Sub cbOK_Click()


SaveSetting App.EXEName, "checkboxes\frmdvan", "chkA", chkA.Value


End Sub

----------


## MartinLiss

I'm not sure I understand, but let me point out that 

VB Code:
AValue = GetSetting("TestSSvr", "checkboxes\frmdvan", "chkA", "vbUnchecked")

will look in the Registry for *HKEY_CURRENT_USER|Software|VB and VBA Program Settings|TestSSvr|checkboxes\frmdvan|frmdvan* and if it doesn't find that entry it will return the string vbUnchecked because you have that value in quotes. If you aren't aware vbUnchecked is a built-in VB constant with a value of 0, so if you want to use it with your checkbox you should remove the quotes.

Similarly in your second example where you have

VB Code:
chkA.Value = GetSetting(App.EXEName, "checkboxes\frmdvan", "chkA", "")
I think that probably what you want is

VB Code:
chkA.Value = GetSetting(App.EXEName, "checkboxes\frmdvan", "chkA", vbUnchecked)

If that doesn't fix your problem you should use regedit and look for *HKEY_CURRENT_USER|Software|VB and VBA Program Settings* and see if it has a sub-folder named TestSSvr (and/or whatever your exe name is) and see what values are stored in that/those subfolders.

----------


## draven2kg

It was a typo. But what i want is..


Public Function GetValue()

Dim AValue

AValue = GetSetting("TestSSvr", "checkboxes\frmdvan", "chkA", "")

GetValue = AValue

End Function


To give me the current value of the checkbox. ...vbUnchecked just gives me 0 all the time.

----------


## MartinLiss

Did you look in the Registry to see what is there?

----------


## MartinLiss

Put this in a form and you'll see that it works.

VB Code:
Option Explicit
 Private Sub Form_Load()
     chkA.Value = GetSetting("TestSSvr", "checkboxes\frmdvan", "chkA", vbUnchecked)
    
End Sub
  Private Sub Form_Unload(Cancel As Integer)
     SaveSetting "TestSSvr", "checkboxes\frmdvan", "chkA", chkA.Value
 End Sub

----------


## draven2kg

I have this code in my ASP page..it makes a call to my active x component which in turn checks the registry to find the value of the key.


             Dim CallChkAudit
	Dim AuditOnOff

	Set CallChkAudit = Server.CreateObject("SummitSvr.WebSvr")

	AuditOnOff = CallChkAudit.GetAuditValue




This is the code in my active x component...and it reads the registyr to find the value of the checkbox. Which should on default be turned on...

This is where i keep getting a 0.....and if i use

AValue = GetSetting("TestSummitSvr", "checkboxes\frmTestAdvanced", "chkAudit", vbUnchecked) i get blank  

Public Function GetAuditValue()

Dim AValue

AValue = GetSetting("TestSummitSvr", "checkboxes\frmTestAdvanced", "chkAudit", vbUnchecked)


GetAuditValue = AValue


End Function




this 2 sections of code are  in my exe form which saves the value of the checkbox and the other which loads the current value for the check box


Private Sub cbOK_Click()

    'Use to get value from registry
    'Dim value As String
    'value = GetSetting("TestSummitSvr", "checkboxes\frmTestAdvanced", "chkAudit", "Default")



    'Saves checkbox value of form to registry
    SaveSetting App.EXEName, "checkboxes\frmTestAdvanced", "chkAudit", chkAudit.Value


End Sub



Private Sub Form_Load()

    chkAudit.Value = GetSetting(App.EXEName, "checkboxes\frmTestAdvanced", "chkAudit", vbUnchecked)



End Sub

----------


## MartinLiss

Sorry but sitting here I can't tell what the problem is.

----------


## draven2kg

The following code is in a class module in a dll :

I have a dll that makes a call to get a checkboxes value from my registry.
However i keep getting a 0 which i think is the default value or a vbUnchecked value. What am i doing wrong?




visual basic code:--------------------------------------------------------------------------------Public Function GetAuditValue() As Variant

Dim AValue As Variant


AValue = GetSetting("TestSummitSvr", "checkboxes\frmTestAdvanced", "chkAudit", vbUnchecked)



    Dim fso2, txtfile2

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set txtfile = fso.OpenTextFile("C:\test\testfile2.txt", 8, True)

   txtfile.WriteLine AValue
   txtfile.Close



GetAuditValue = AValue


End Function--------------------------------------------------------------------------------



This prints a 0 to my text file even though the key in my registry is a 1









The codes below are in an exe on a form that has a check box that i have to save and get the settings from registry. 




visual basic code:--------------------------------------------------------------------------------Private Sub cbOK_Click()

SaveSetting App.EXEName, "checkboxes\frmTestAdvanced", "chkAudit", chkAudit.Value

End Sub--------------------------------------------------------------------------------




I use this code to get the checkboxes registry value when form loads from registry....
visual basic code:--------------------------------------------------------------------------------Private Sub Form_Load()

chkAudit.Value = GetSetting(App.EXEName, "checkboxes\frmTestAdvanced", "chkAudit", vbUnchecked)

End Sub

--------------------------------------------------------------------------------

Here is a slight change.

----------


## MartinLiss

"This prints a 0 to my text file even though the key in my registry is a 1"

That doesn't seem possible. Put a breakpoint on the Set fso line and see what is in AValue.

----------


## draven2kg

When i take this code and put it in a form with a command botton it works good. Could it be something that is happening b/w the dlls ?




Public Function GetAuditValue() As Variant

Dim AValue As Variant


AValue = GetSetting("TestSummitSvr", "checkboxes\frmTestAdvanced", "chkAudit", vbUnchecked)



Dim fso2, txtfile2

Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.OpenTextFile("C:\test\testfile2.txt", 8, True)

txtfile.WriteLine AValue
txtfile.Close



GetAuditValue = AValue


End Function

----------


## MartinLiss

I don't know. You are probably better off if you post a question in one of the other forums.

----------


## draven2kg

Git any suggestions how else i can save a checkboxes value?

----------


## BubbleLife

> Git any suggestions how else i can save a checkboxes value?



It's a bit.. old fashioned, but it's how all my programs work. Hope it can be some help, if not, well, I tried (I'm a bit of a noob at VB though)  :Big Grin:

----------


## Ebiru

so how could u dele them? can u show me like a path to which i can find them

----------


## MartinLiss

> so how could u dele them? can u show me like a path to which i can find them


Post #1 says where they are. You can either manully delete them or use DeleteSetting

DeleteSetting AppName, [Section], [Key]

----------


## Ebiru

> Post #1 says where they are. You can either manully delete them or use DeleteSetting
> 
> DeleteSetting AppName, [Section], [Key]


i see cant find it....whats the actual path....HKEY_CURRENT_USER|Software|VB and VBA Program Settings|YourAppName that doesnt help me.

----------


## MartinLiss

Show me one of the GetSetting or SaveSetting commands that you use.

----------


## Ebiru

```
SaveSetting App.EXEName, name, "badguy", badguy(x).left
```

there is one
name = name of the player

----------


## MartinLiss

So assuming the name of your project is Project1 then you will find an entry in the Registry at

HKEY_CURRENT_USER|Software|VB and VBA Program Settings|Project1

----------


## Ebiru

> So assuming the name of your project is Project1 then you will find an entry in the Registry at
> 
> HKEY_CURRENT_USER|Software|VB and VBA Program Settings|Project1


just saying that still isnt clear to me. can u explain alittle more in detail

----------


## MartinLiss

Have you ever looked inside the Registry? If not then do Start|Run|Regedit and you will see something that looks similar to Windows Explorer. One of the first "folders" will be HKEY_CURRENT_USER. One of it's "sub-folders" will be "Software", etc.

----------


## Ebiru

also is there a way to secure them? so like people dont just go in n edit them

----------


## MartinLiss

No, but you can use any one of several encryption/decryption methods to protect the data.

----------


## Ebiru

> No, but you can use any one of several encryption/decryption methods to protect the data.


may i ask if u can point me to these methods?

----------


## MartinLiss

You can use the Search capability of these forums to do that.

----------


## Ebiru

like what should i search for? encryption ect...?

----------


## MartinLiss

encrypt or encryption would certainly seem to be good choices.

----------


## Ellis Dee

> encrypt or encryption would certainly seem to be good choices.


heh, you're killing me over here.

I'm confused by this whole thread. I thought the code base was for finished solutions. Why devote a thread to standard VB functions? It seems about as useful as devoting a thread to the InStr() function.

----------


## MartinLiss

You are thinking of the Utility Bank. The Code Bank is for useful snippets of code. If you want to continue this discussion please either post in Forum Feedback or PM me.

----------


## RPK58

It seems to me that you are using different application names in GetSetting and SaveSetting functions-

SaveSetting App.EXEName, "checkboxes\frmTestAdvanced", "chkAudit", chkAudit.Value

and 

GetSetting("TestSummitSvr", "checkboxes\frmTestAdvanced", "chkAudit", vbUnchecked)

I don't know the full logic of your program, but if this is true then you are not reading from the same location in regisrty as you are writing to, unless the App.EXEName is "TestSummitSvr"






> The following code is in a class module in a dll :
> 
> I have a dll that makes a call to get a checkboxes value from my registry.
> However i keep getting a 0 which i think is the default value or a vbUnchecked value. What am i doing wrong?
> 
> 
> 
> 
> visual basic code:--------------------------------------------------------------------------------Public Function GetAuditValue() As Variant
> ...

----------


## Flodi

You can use this information for checkboxes too ...just use you'r mind...

This is how i did it ...

There is a TextBox called Text1 on my form and it is invisible ... and there is a checkbox and a timer (value = 10)

And this is the code :



```
Private Sub Check1_Click()
If Check1.Value = 0 Then Text1.Text = "UnChecked"
If Check1.Value = 1 Then Text1.Text = "Checked"
End Sub

Private Sub Form_Load()
Text1.Text = GetSetting(App.EXEName, "textboxes", "text1", "")

End Sub

Private Sub Form_Unload(Cancel As Integer)
SaveSetting App.EXEName, "textboxes", "text1", Text1.Text
End Sub

Private Sub Timer1_Timer()
If Text1.Text = "Checked" Then Check1.Value = 1
Timer1.Enabled = False
End Sub
```



P.S

Anyone knowes how can i save the content of a listbox in registry ??

----------


## MartinLiss

Sure. Loop through the entries saving them to a string separated by something that's not likely to be in any of them like "||" and then write that string to the registry. When you want to restore them use Split on what GetSetting returns.

----------


## mixman

Hi!

This is really useful, thanks!

My question is, how can i make if statement on loading that checks if this value is already saved?

I want to do something like this


If SOMETHING THAT CHECKS IF VALUE IS SAVED <> "" Then
Command1.enabled = false
Else
End If

Basicly i want to check that if value is already saved then disable save button...



Regards

----------


## MartinLiss

As I mentioned in post#1, the 4th parameter of GetSetting is the default so you could do this.



```
If GetSetting(App.EXEName, "textboxes", "text1", "") <> "" Then
    Command1.enabled = false
End If
```

----------

