Read form Registry (1 Viewer)

gstylianou

Registered User.
Local time
Today, 03:32
Joined
Dec 16, 2013
Messages
357
Hi all,

I'm trying to create a routine into my database on form Load to read the value data from my file. My application is stored into HKEY_CURRENT_USER\Software\CSApp path and there is another file into CSApp folder which has the value data 100. This file is named as SNPLUS and it's REG_DWORD file format and as i already told, has the value data: 100 which is Hexadecimal.

Is it possible to read this number directly form access vba?

Waiting any help...thanks in advanced
 

Ranman256

Well-known member
Local time
Yesterday, 20:32
Joined
Apr 9, 2015
Messages
4,339
try:
SaveSetting "MyApp","Startup", "Top", 75

GetSetting( appname, section, key [ , default ] )
 

isladogs

MVP / VIP
Local time
Today, 01:32
Joined
Jan 14, 2017
Messages
18,209
AFAIK the functions SaveSetting & GetSetting only work for registry entries located at HKEY_CURRENT_USER\SOFTWARE\VB and VBA Program Settings.

Your registry entry almost fits that so it may work...
....but if not I have a more general function you can use
 

gstylianou

Registered User.
Local time
Today, 03:32
Joined
Dec 16, 2013
Messages
357
Already i tried the following without any result....Please could anyone attached any sample accdb file?

Private Sub Form_Load()
Dim RegObj, RegKey
Set RegObj = CreateObject("WScript.Shell")
RegKey = RegObj.RegRead(" HKEY_CURRENT_USER \ Software \ CSApp\SNPLUS.REG_dword")

If RegKey = "" Then
MsgBox "There is no registry value for this key!"
Else
MsgBox "Registry key value is " & RegKey & "!"
Set RegObj = Nothing
End If
End Sub
 

Attachments

  • 2.JPG
    2.JPG
    39.5 KB · Views: 73
  • Capture.JPG
    Capture.JPG
    25.8 KB · Views: 52

isladogs

MVP / VIP
Local time
Today, 01:32
Joined
Jan 14, 2017
Messages
18,209
Almost the same as my own code
A few changes needed:
1. Change one line to remove REG_dword and all the spaces you added
Code:
RegKey = RegObj.RegRead("HKEY_CURRENT_USER\Software\CSApp\SNPLUS")
2. Move the line Set RegObj = Nothing AFTER End If so it always gets cleared whatever the RegKey value
3. The code will error if the reg key doesn't exist so you need error handling.
4. You didn't define your variables so they default to variant

Modified code:

Code:
Private Sub Form_Load()

On Error GoTo Err_Handler

    Dim RegObj As Object, RegKey As String
    
    Set RegObj = CreateObject("WScript.Shell")
    RegKey = RegObj.RegRead("HKEY_CURRENT_USER\Software\CSApp\SNPLUS")

    If Nz(RegKey, "") <> "" Then
        MsgBox "Registry key value is " & RegKey & "!"
    Else
        MsgBox "There is no registry value for this key!"
    End If
    
    Set RegObj = Nothing
    
Exit_Handler:
    Exit Sub
    
Err_Handler:
    MsgBox "Error " & Err.Number & vbCrLf & Err.Description
    Resume Exit_Handler
End Sub
 
Last edited:

gstylianou

Registered User.
Local time
Today, 03:32
Joined
Dec 16, 2013
Messages
357
Almost the same as my own code
A few changes needed:
1. Change one line to remove REG_dword and all the spaces you added
Code:
RegKey = RegObj.RegRead("HKEY_CURRENT_USER\Software\CSApp\SNPLUS")
2. Move the line Set RegObj = Nothing AFTER End If so it always gets cleared whatever the RegKey value
3. The code will error if the reg key doesn't exist so you need error handling.
4. You didn't define your variables so they default to variant

Modified code:

Code:
Private Sub Form_Load()

On Error GoTo Err_Handler

    Dim RegObj As Object, RegKey As String
    
    Set RegObj = CreateObject("WScript.Shell")
    RegKey = RegObj.RegRead("HKEY_CURRENT_USER\Software\CSApp\SNPLUS")

    If Nz(RegKey, "") <> "" Then
        MsgBox "Registry key value is " & RegKey & "!"
    Else
        MsgBox "There is no registry value for this key!"
    End If
    
    Set RegObj = Nothing
    
Exit_Handler:
    Exit Sub
    
Err_Handler:
    MsgBox "Error " & Err.Number & vbCrLf & Err.Description
    Resume Exit_Handler
End Sub


Dear friend,

First of all i would like to thank toy very much for your help. Its working fine, but....i wanted to read the value data which i assigned into the REG_DWORD file....*see the picture please...Is that possible? And something last, how can i write into this registry path a new string file (eg. MyApp. REG_SZ)? I tried to do that but i'm sure i'm not writing the correct file extension..
 

Attachments

  • Capture.JPG
    Capture.JPG
    25.8 KB · Views: 79
Last edited:

isladogs

MVP / VIP
Local time
Today, 01:32
Joined
Jan 14, 2017
Messages
18,209
DWORD keys can be entered / viewed either as hexadecimal or decimal base

The code I gave you will display the DECIMAL version which is 268566576
To convert it to HEXACDECIMAL use the Hex function

e.g.
Code:
MsgBox "Registry key value is " & Hex(RegKey)

or you could show both formats:
Code:
MsgBox "Registry key value is " & RegKey & "; Hex " & Hex(RegKey)

The procedure below includes code to write / read & delete registry keys with examples. All are very similar

Code:
'-------------------------------------------------------------------
' Procedure : RegWriteStringValue
' Author    : Patrick Wood http://gainingaccess.net/
' Date      : 3/22/2010
' Purpose   : Write new subkey(s), StringValueName, and StringValue to the Registry, read the value(s) and delete the subkey(s)
'--------------------------------------------------------------------------

Sub RegWriteStringValue()

On Error GoTo ErrHandler

'The following code creates a key and two values, reads them, and deletes them.

Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")

WshShell.RegWrite "HKCU\Software\Mendip Data Systems\Test\ZZZZ", "abcde", "REG_SZ"
WshShell.RegWrite "HKCU\Software\Mendip Data Systems\Test\DW", 268566576, "REG_DWORD"
'WshShell.RegWrite "HKCU\Software\Mendip Data Systems\Test\", 1, "REG_BINARY"

Debug.Print WshShell.RegRead("HKCU\Software\Mendip Data Systems\Test\ZZZZ")
Debug.Print WshShell.RegRead("HKCU\Software\Mendip Data Systems\Test\DW")
'Debug.Print WshShell.RegRead("HKCU\Software\Mendip Data Systems\Test\")

'delete keys starting with innermost
'use trailing '\' as shown
WshShell.RegDelete "HKCU\Software\Mendip Data Systems\Test\ZZZZ"
'WshShell.RegDelete "HKCU\Software\Mendip Data Systems\Test\"
'WshShell.RegDelete "HKCU\Software\Mendip Data Systems\"

Set WshShell = Nothing

Exit_ErrHandler:
    On Error Resume Next
    Exit Sub
    
ErrHandler:
        MsgBox "Error " & Err.Number & " in RegWriteStringValue procedure: " & Err.Description, vbOKOnly + vbCritical
    Resume Exit_ErrHandler

End Sub

Suggest you test on dummy values (especially when deleting keys)

NOTE:
The HKEY_CURRENT_USER (HKCU) hive can always be edited
THe KKEY_LOCAL_MACHINE (HKLM) hive can only be edited if you are running Access as an administrator

Hope that helps
 

Users who are viewing this thread

Top Bottom