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
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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

726 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