darrenr
asked on
Using VBA to read the registry ?
I need to be able to read some registry settings from
MS Access but can't find any hints on how to do this.
MS Access but can't find any hints on how to do this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-) and i thought that i was posting a big chunk of text
waty: How about a little example of how to use it. Or have you too been overwhelmed by the complexity of this information.
If siabods approach works I would go for that one. Seems much easier. In fact I will try it as soon as possible.
If siabods approach works I would go for that one. Seems much easier. In fact I will try it as soon as possible.
ASKER
I will be trying both of these suggestions out tomorrow. Thanks.
---------
Accessing the registry
The first step in accessing the registry collections is to declare a node object variable and connect it to a registry node:
Dim nodesTop As New CRegNode‘ Connect to first-level node by namenodesTop.Create “Software\VB and VBA Program Settings”
The Create method can be used in several ways. Its signature looks like this:
Sub Create(vIndex As Variant, _ Optional RootKey As Long = HKEY_CURRENT_USER, _ Optional AccessRights As Long = KEY_ALL_ACCESS)
You’ll need to provide a RootKey parameter to connect to anything other than HKEY_CURRENT_USER. The vIndex parameter is a Variant so that it can take either a string or numeric argument. It can be a key name (as shown above), a remote computer name, or the handle of a previously opened key.
You can also connect a registry node by using the default Key property. Here are some examples:
‘ Connect HKEY_CLASSES_ROOT nodenodesTop.Key = HKEY_CLASSES_ROOT‘ Connect VBCore.CAbout node in current node (HKEY_CLASSES_ROOT)nodesTo
Once you’ve connected to a node, you can read its item values by name:
v = node.Items(“Bytes”)
Or you can get an item’s value by the item’s position number:
v = node.Items(1)
Either way you get back a Variant that could contain a Long, a String, or an
array of Bytes. You have to check the type with VarType to decide what to do with it. The sample simply converts any type to a string, but real programs might have to do something more sophisticated—especially with binary data stored in Byte arrays.
You can also set item values of any registry type. Here’s how to add binary data named Bytes:
Dim ab() As Byte‘ Add bytes itemab = “The bytes”node.AddItem ab, “Bytes”
You can also add strings containing environment variables such as this string named ExpandString:
node.AddItem “A %TEMP% string”, “ExpandString”
The string will be saved in the registry as is, with the percent signs intact, but it will be extracted with the TEMP environment variable expanded. Here are a few more examples showing how item values can be added to named nodes:
node(“SecondLevel1”).AddIt
The first argument is the value of the item, the second is its name. If you don’t give a name, the value will become the default value, which will always be stored as a string even if it isn’t one.
You can remove items by name or by position:
node.RemoveItem 1node.RemoveItem “String”
You can also remove nodes by name or position, but whether you succeed will depend on whether the node has children. Here’s an attempt to remove a childless node:
f = nodesTop.RemoveNode(“First
This call will fail if the specified node has children. You might want to specify the optional argument as False to avoid accidentally deleting large branches from the registry. The default is to remove the node and all its children. Note that a node can’t remove itself.