Deleting a registry key

Posted on 2006-05-09
Last Modified: 2013-12-25
I am trying to delete a registry key from my VB app. I am using following function to delete the registry key.

Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

I am getting error code 2, and after looking in the WinError.h, it stands for ERROR_FILE_NOT_FOUND. I have given Full control to everyone for all the registry operations and for this hive as well.

Question by:deming
    LVL 12

    Expert Comment


    '=================================[ FORM CODES ]=================================
    Private Sub Command1_Click()
      If DeleteRegKey(HKEY_LOCAL_MACHINE, "\Software\MyApplication\MyValue") Then
        MsgBox "Registry key deleted"
        MsgBox "Failed to delete registry key"
      End If
    End Sub
    '=================================[ FORM CODES ]=================================

    '=================================[ MODULE CODES ]=================================
    Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

    Enum RegHive
       HKEY_CLASSES_ROOT = &H80000000
       HKEY_CURRENT_USER = &H80000001
       HKEY_LOCAL_MACHINE = &H80000002
    End Enum

    Public Function DeleteRegKey(ByVal hKey As RegHive, ByVal sPath As String) As Long
     Dim lResult As Long
     lResult = RegDeleteKey(hKey, sPath)
     DeleteRegKey = lResult
    End Function
    '=================================[ MODULE CODES ]=================================

    Author Comment

    I am also using the same function. but i am getting the return value as 2. Can u explain me why??
    LVL 14

    Accepted Solution

    Under WindowsNT environment like Win2000 or XP it doesn't work if the key contains subkeys. I think that's why ur getting error. So, u need to delete the subkeys first followed by the parent key. See:

    You can try this:


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    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…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    755 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

    23 Experts available now in Live!

    Get 1:1 Help Now