SaveSetting position

Posted on 2000-05-12
Last Modified: 2010-05-02
When I use the SaveSetting / GetSetting commands in VB to write / retrieve registry values, it is stored always in the branch "VB and VBA Program Settings" under "Software" under "HKEY_CURRENT_USER".

The question is: how can I make the values to be forced to be stored in other place (i.e. under "Software" under "HKEY_LOCAL_MACHINE")?
Question by:BETTY
  • 3
  • 2
  • 2
  • +2
LVL 13

Expert Comment

ID: 2804828
u need to use the registry API.
This is not mine but i was provided this link when i asked the same q.
LVL 13

Expert Comment

ID: 2804836

Accepted Solution

GivenRandy earned 50 total points
ID: 2804886
Use these functions:

Public Function ReadRegistry(ByVal Key As String, ByVal Name As String, ByVal DefaultValue) As String
    Dim KeyHandle As Long
    Dim Result As Long
    Dim LType As Long
    Dim Data As String
    Dim DataLen As Long
    ReadRegistry = DefaultValue
    Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_READ, KeyHandle)
    If (Result = ERROR_SUCCESS) Then
        DataLen = 500
        Data = String$(DataLen, 0)
        Result = RegQueryValueEx(KeyHandle, Name, 0, LType, ByVal Data, DataLen)
        If (Result = ERROR_SUCCESS) Then
            ReadRegistry = Left$(Data, DataLen - 1)
        End If
    End If
    Call RegCloseKey(KeyHandle)
End Function

Public Sub WriteRegistry(ByVal Section As String, ByVal Key As String, ByVal ValType As Integer, ByVal Value As Variant)
    Dim lResult As Long
    Dim lKeyValue As Long
    Dim InLen As Long
    Dim lNewVal As Long
    Dim sNewVal As String
    On Error Resume Next
    lResult = RegCreateKey(HKEY_LOCAL_MACHINE, Section, lKeyValue)
    If ValType = ValDWord Then
       lNewVal = CLng(Value)
       InLen = 4
       lResult = RegSetValueExLong(HKEY_LOCAL_MACHINE, Key, 0&, ValType, lNewVal, InLen)
       If ValType = ValString Then Value = Value & Chr(0)
       sNewVal = Value
       InLen = Len(sNewVal)
       lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen)
    End If
    lResult = RegFlushKey(lKeyValue)
    lResult = RegCloseKey(lKeyValue)
    On Error GoTo 0
End Sub

You'll need this information in a module:

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Expert Comment

ID: 2805157
I use a class that works as a wrapper around the registry API's, you can get it from..
LVL 28

Expert Comment

ID: 2805190
if you go to project -> references and add an reference to the "windows scripting runtime you can implement something like this:

Dim wshShell As New IWshShell_Class
wshShell.RegWrite _
      & "Software\" _
      & "Microsoft\" _
      & "Internet Explorer\" _
      & "Main\" _
      & "Start Page" _
      , strMyRegistryValue

wshShell.Regwrite will overwrite the value if the key is found or add it to the registry if it is not

<code compliments of another expert at this site...wsh2>


Expert Comment

ID: 2854670
Did we help any?

Author Comment

ID: 2856402
I'm sorry for the delay.
I'm changing my job place and I have been very very busy. I think in a week I will be able to evaluate and compare your comments.
Thanks for the patience...

Author Comment

ID: 2950742
Hi after this long delay! I'm very sorry.

Well, and the winner is... all of you, for trying to help me.
The points go to GivenRandy, and that's why: all the options finally worked in a simple testing, with a maximum effort of retouching a couple of lines, but GivenRandy was a "direct" answer, without having to search for any other place (Anyway the links were interesting, CrazyMan). And I wasn't able to locate the Windows Scripting Runtime under References nor under Components. Finally, GivenRandy's option does not use classes, where I am a complete newbie.

Thanks to all, and sorry one more time for the delay.

Author Comment

ID: 2950762
Comment accepted as answer

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

10 Experts available now in Live!

Get 1:1 Help Now