Solved

SaveSetting position

Posted on 2000-05-12
9
322 Views
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")?
0
Comment
Question by:BETTY
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Expert Comment

by:crazyman
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.
http://homepage.eircom.net/~carafa/VBSamples/VBAll/shellexecute.zip
0
 
LVL 13

Expert Comment

by:crazyman
ID: 2804836
0
 
LVL 9

Accepted Solution

by:
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)
    Else
       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
-----
0
 
LVL 7

Expert Comment

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

http://www.cyd.liu.se/~freqv416/clsRegistry.cls
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:AzraSound
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 _
"HKEY_CURRENT_USER\" _
      & "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>


     
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2854670
Did we help any?
0
 
LVL 1

Author Comment

by:BETTY
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...
0
 
LVL 1

Author Comment

by:BETTY
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.
0
 
LVL 1

Author Comment

by:BETTY
ID: 2950762
Comment accepted as answer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) 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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

895 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

18 Experts available now in Live!

Get 1:1 Help Now