Visual Basic Source Code - Writing to the Registry

For: VB4, VB5, VB6

Visual Basic, by default using GetSetting and SaveSetting allows reading and writing only to the VB and VBA Programs key under HKEY_Current_User\Software. There can be any number of reasons to want to read or write other portions of the registry, and that can be accomplished using API calls. There are two typical ways that this is done; the first is with a Class Module, and the second is with a regular function or sub. I personally prefer the function/sub approach, which is shown in the code below. My earliest versions of this code were published on Compuserve in 1996, and have evolved steadily since then.

With this code, you can write strings to any portion of the registry for which you have permissions. A Read Function is on another page (link at bottom). First you need a number of declares. Not all of these are used for every registry access, but it will not hurt to include them in the declarations portion of the .BAS module that contains your function/subroutine. Copy/Paste from the box below:



Now we are ready for the subs themselves. If the value does not exist, it will be created. This sub will write string (REG_SZ) values:


I prefer a separate subroutine to accept numeric values. This sub will write number (REG_DWORD) values:


Go to the VB Source Code index or Go to the Registry Read Code

Comments? Send mail to donb@arcatapet.net
Snail Mail: Arcata Pet, 600 F Street, Arcata, CA 95521-6301 USA
Webspace provided by and ©Copyright 1994-2013 by Arcata Pet Supplies. All Rights Reserved.