VB TIP: Using the GetSetting and SaveSetting Commands

by Sam Huggill

Manipulate the registry through Visual Basic.

Option Explicit

Public Const ThisApp = "My Company"
Public Const ThisKey = "My Prog Name"

Public Declare Function RegOpenKeyEx _
   Lib "advapi32.dll" _
 Alias "RegOpenKeyExA" (_
         ByVal hKey As Long, _
         ByVal lpSubKey As String, _
         ByVal ulOptions As Long, _
         ByVal samDesired As Long, _
         phkResult As Long) As Long

Public Declare Function RegQueryValueEx _
    Lib "advapi32.dll" _
 Alias "RegQueryValueExA" ( _
         ByVal hKey As Long, 
         ByVal lpValueName As String, _
         ByVal lpReserved As Long, _
         ByVal lpType As Long, _
         ByVal lpData As Any, _
         lpcbData As Long) As Long

Public Declare Function RegCloseKey _
   Lib "advapi32.dll" _
         ByVal hKey As Long) As Long

Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CURRENT_USER = &H80000001
Public Const READ_CONTROL = &H20000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_NOTIFY = &H10
Public Const SYNCHRONIZE = &H100000
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_MORE_DATA = 234
Public Const REG_SZ = 1

Public Const KEY_READ = ((READ_CONTROL Or _
                          KEY_QUERY_VALUE Or _
                          KEY_ENUMERATE_SUB_KEYS Or _
                          KEY_NOTIFY) And _
                          (Not SYNCHRONIZE))

Public Const KEY_EXECUTE = ((KEY_READ) And _
                            (Not SYNCHRONIZE))

Global Name As String
Global Phone As String
Global PostCode As String
Private Sub cmdLoad_Click()

Dim Name, Phone, PostCode As String

Name = GetSetting(ThisApp, ThisKey, "Name", "")
Phone = GetSetting(ThisApp, ThisKey, "Phone", "")
PostCode = GetSetting(ThisApp, ThisKey, "PostCode", "")

txtName.Text = (Name)
txtPhone.Text = (Phone)
txtPostCode.Text = (PostCode)

End Sub

Private Sub cmdSave_Click()

Dim Name, Phone, PostCode As String

SaveSetting ThisApp, ThisKey, "Name", txtName.Text
SaveSetting ThisApp, ThisKey, "Phone", txtPhone.Text
SaveSetting ThisApp, ThisKey, "PostCode", txtPostCode.Text

End Sub
This article was originally published on Tuesday Nov 19th 2002
Mobile Site | Full Site