Registry writing, reading and deleting

Posted on 2005-04-14
Last Modified: 2011-09-20
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,
Question by:PCMCIA
    LVL 32

    Accepted Solution

    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")
    LVL 8

    Assisted Solution

    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
    LVL 6

    Expert Comment

    split because the first answer is a first correct answer, but indeed, in a multiuser environment, the second is prime code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now