I have this code in a .bas module, when ever i try to use it the value always comes back as false. If i go to regedit and find they key the value is password. I need the value to come back as password.

Please can somebody tell me how to fix this as i need it.

Code:
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, _
    ByRef KeyVal As String) As Boolean
    Dim i As Long
    Dim rc As Long
    Dim hKey As Long
    Dim KeyValType As Long
    Dim tmpVal As String
    Dim KeyValSize As Long
    
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey)
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError
    
    tmpVal = String$(1024, 0)
    KeyValSize = 1024
    
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, KeyValType, tmpVal, KeyValSize)
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError
    


    If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then
        tmpVal = Left(tmpVal, KeyValSize - 1)
    Else
        tmpVal = Left(tmpVal, KeyValSize)
    End If
    


    Select Case KeyValType
        Case REG_DWORD


        For i = Len(tmpVal) To 1 Step -1
            KeyVal = KeyVal + Format(Hex(Asc(Mid(tmpVal, i, 1))), "00")
        Next
        KeyVal = Format$("&h" + KeyVal)
        Case REG_SZ
        KeyVal = tmpVal
    End Select

GetKeyValue = True
rc = RegCloseKey(hKey)
Exit Function

GetKeyError:
GetKeyValue = False
rc = RegCloseKey(hKey)
End Function
thanks Chris1990