• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Registry writing, reading and deleting

Hi, i made a simple program in visual basic 6, that calls a database, stored on a server in a network where i work. What i need is simple, i have add another form to my project where i have put a text box and a command button, i want to put the address of the database in that text box and when i press the cmd button i want the program to store it on the registry where the program is running. I want to store the path of my database, so when i run the program the first thing it does, is check the registry previously created to find the database on the network. I would also like to know how to delete it with another cmd button.

Thanks a lot,
2 Solutions
Easiest way is to use VB's built-in registry functions, SaveSetting, GetSetting, DeleteSetting:

Const APP_NAME = "My Application"

'Save a setting to the registry
Call SaveSetting(APP_NAME, "My Section", "My Key", "String Value")

'Get a setting from the registry
Text1.Text = GetSetting(APP_NAME, "My Section", "My Key", "Default Value")

'Delete the setting
Call DeleteSetting(APP_NAME, "My Section")
Erick37's solution does everything in HKEY_CURRENT_USER, which means that if someone logs on under a different user they wont be able to access the settings. To do it in HKEY_LOCAL_MACHINE you can add the "Windows Script Host Object Model" reference to your project and use:

Public Function GetSett(ByVal strSection As String, ByVal strKey As String, ByVal strDefault As Variant) As Variant
On Error GoTo errhandlr
    Dim MyReg As New IWshRuntimeLibrary.WshShell
    GetSett = MyReg.RegRead("HKLM\Software\YourFolder\" & strSection & "\" & strKey)
    Set MyReg = Nothing
    Exit Function

    GetSett = strDefault
    Set MyReg = Nothing
End Function

Public Function SaveSett(ByVal strSection As String, ByVal strKey As String, ByVal strSetting As String)
    Dim MyReg As New IWshRuntimeLibrary.WshShell
    MyReg.RegWrite "HKLM\Software\YourFolder\" & strSection & "\" & strKey, strSetting
    Set MyReg = Nothing
End Function

Public Function DelSett(ByVal strSection As String, ByVal strKey As String)
    Dim MyReg As New IWshRuntimeLibrary.WshShell
    MyReg.RegRead ("HKLM\Software\YourFolder\" & strSection & "\" & strKey)
    Set MyReg = Nothing
End Function
split because the first answer is a first correct answer, but indeed, in a multiuser environment, the second is prime code.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now