Results 1 to 5 of 5
  1. #1
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Examples for working with the Registry (VB-6, VBA)

    Hello Folks

    Can anyone give me an example in VB 6 on how to change a Registry key string value?

    I know what the key is, I just need an example to change its value.

    Also do you think that all Windows versions share the same key names in the registry?

    Thanks a million.

    Wassim
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Examples for working with the Registry (VB-6, VBA)

    Here is some code I copied from one of my workbooks and then modified to fit this post. It has not been tested as modified.

    <pre>Option Explicit

    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 RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long


    Public Sub ChangeReg()
    Dim lResult As Long, phkUser As Long
    Dim strRegKey As String, strWork As String
    strRegKey = "Hive containing the key"
    lResult = RegOpenKeyEx(HKEY_CURRENT_USER, strRegKey, 0, KEY_ALL_ACCESS, phkUser)
    If lResult <> ERROR_SUCCESS Then
    MsgBox "Can't access registry key"
    End
    End If
    strWork = "New Key Value" & Chr(0)
    lBufLen = Len(strWork)
    lResult = RegSetValueEx(phkUser, "KeyToChange", 0, REG_SZ, strWork, lBufLen)
    If lResult <> ERROR_SUCCESS Then
    strMsg = "Can not update the System Registry. " & _
    "Error Code " & lResult & ". " & " Program failed."
    MsgBox strMsg, vbOKOnly + vbCritical, strRegErrorTitle
    End
    End If

    End Sub
    </pre>

    Legare Coleman

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Examples for working with the Registry (VB-6, VBA)

    It depends on what part of the registry you're trying to change. Have you looked at the GetSetting and SaveSetting functions in VB/VBA? Here's an example from the VB help:

    ' Variant to hold 2-dimensional array returned by GetSetting.
    Dim MySettings As Variant
    ' Place some settings in the registry.
    SaveSetting "MyApp","Startup", "Top", 75
    SaveSetting "MyApp","Startup", "Left", 50

    Debug.Print GetSetting(appname := "MyApp", section := "Startup", _
    key := "Left", default := "25")

    DeleteSetting "MyApp", "Startup"
    Charlotte

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Examples for working with the Registry (VB-6, VBA)

    Legare,
    Additionally, you would need to define the various constants you've used.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Examples for working with the Registry (VB-6, VBA)

    You also can use the Windows Script Host. See, for example, <!post=Post #121209,121209>Post #121209<!/post>. And if that doesn't make you nervous about VBS attachments, nothing will. <img src=/S/smile.gif border=0 alt=smile width=15 height=15> You can find some more examples with a Lounge search for wscript regread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •