VBA code not working

azhar2006

Registered User.
Local time
Yesterday, 16:31
Joined
Feb 8, 2012
Messages
297
Hello all
I try to execute this code, but the message does not appear and confirms that the key exists. Please help.
Thanks >>>AZ
Code:
Private Sub Command2_Click()

Dim MN As Long
Dim DN As Long
Dim EE As Long
Dim APW1 As Variant
Dim UPW2 As Variant

MN = HKEY_CLASSES_ROOT
EE = 1
APW1 = "003"
UPW2 = "003"

DN = QueryValue(MN, "Light\A0\A20\EE", "EE")
If DN = 1 Then
MsgBox "key is present"
End
End If
APV = Me.Text0

CreateNewKey MN, "Light\A0\A20\EE"
CreateNewKey MN, "Light\A0\A20\AP"
CreateNewKey MN, "Light\A0\A20\UP"
SetKeyValue MN, "Light\A0\A20\EE", "EE", EE, 1
SetKeyValue MN, "Light\A0\A20\AP", "APW1", APW1, 1
SetKeyValue MN, "Light\A0\A20\UP", "UPW2", UPW2, 1

End Sub
 
Last edited:
Remove that odd "End" line. I tested some code with that line - it compiles but code does not work with it.

QueryValue() appears to be .net function, not VBA.
 
Last edited:
Seems to be something to with registry?, plus APV is never used? or declared :(
 
According to the link that NG provided, this function only applies to ASP.NET pages. If this isn't running in that context, it won't work. Since Access Web was discontinued some time ago, I would think it would be hard to get into that context unless you had an older version of Access.

Also as correctly noted by June7, your isolated "End" statement that follows the MsgBox statement isn't helping. In fact, I'm rather surprised that it compiled at all, as the "End If" would normally be cut off from its "IF" command, leading to compilation difficulties. I would expect that you should get an "unbalanced If" (or "missing End If") error.
 
is HKEY_CLASSES_ROOT a constant in the expression:

MN = HKEY_CLASSES_ROOT

if not try enclosing HKEY_CLASSES_ROOT in double quote (as string).
 
' Variable = QueryValue(Location, KeyName, ValueName)
' Location must equal HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_lOCAL_MACHINE

So why do you have the first parameter as Long? :(
 
Location must equal HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_lOCAL_MACHINE, , HKEY_USERS

IIRC only the HKCU hive can be edited from Access if it is opened 'normally'
If you need to edit the other hives such as HKLM, this requires Access to be run as an administrator.
 
' Variable = QueryValue(Location, KeyName, ValueName)
' Location must equal HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_lOCAL_MACHINE

So why do you have the first parameter as Long? :(
This link may explain the reason: RegistryHive Enum (Microsoft.Win32) | Microsoft Docs
Although the quoted values are for a 'foreign machine', the hive values for a local machine are also long integer numbers
 
Yes, OP finally posted some code in a DB that had them as constants. :(
Is that what they call drip feeding?
 
I've just opened his app in post #11 though not studied it
This code is from one of my own 'template' databases:

Rich (BB code):
Option Compare Database
Option Explicit

'This module is used to grab registry settings through VBA using WMI

Public Enum Hive
  HKEY_CLASSES_ROOT
  HKEY_CURRENT_USER
  HKEY_LOCAL_MACHINE
  HKEY_USERS
  HKEY_CURRENT_CONFIG
End Enum

'---------------------------------------------------------------------------------------
' Module    : modRegistryNEW
' DateTime  : 26/07/2015
' Authors   : Colin Riddington (except where stated otherwise)
' Website   : http://www.mendipdatasystems.co.uk
' Purpose   : This module is used to grab registry settings through VBA using WMI
' Copyright : The code in theis module MAY be altered and reused in your own applications
' Updated   : Octover 2017
'---------------------------------------------------------------------------------------

'#######################################
Public Function GetHive(hivetype As Hive) As Variant
' return enumerated value depending on the hive chosen
  Select Case hivetype
    Case 0
      GetHive = &H80000000  ' HKEY_CLASSES_ROOT
    Case 1
      GetHive = &H80000001  ' HKEY_CURRENT_USER
    Case 2
      GetHive = &H80000002  ' HKEY_LOCAL_MACHINE
    Case 3
      GetHive = &H80000003  ' HKEY_USERS
    Case 4
      GetHive = &H80000005  ' HKEY_CURRENT_CONFIG
  End Select
End Function
 
Here the message should appear to me. why do not you show ?
i think Nobody want to get involved, since you are messing with the registry?
wrong code can havoc your OS!
 
Removing the bad line just allows code to run. Didn't say it would provide result you want.

I did look at file. First I commented the lines that modify registry then ran code. Runs without error. DN is equal to 0 so apparently key is not found. Should it be?

If you are just going to create and set keys anyway, why check if one of them exists?
 
Removing the bad line just allows code to run. Didn't say it would provide result you want.

I did look at file. First I commented the lines that modify registry then ran code. Runs without error. DN is equal to 0 so apparently key is not found. Should it be?

If you are just going to create and set keys anyway, why check if one of them exists?
To make sure that the other part of the code in the database containing the tables was accepted by the computer
 
I have no problem about editing the registry using my own code ...
Sorry but I'm not prepared to test your code in case it causes problems and I don't have time to spend fixing issues on my PC

Did you understand my comments in post #10?
You are trying to write to the HKEY_CLASSES_ROOT hive (which I really wouldn't recommend).
In order for that to have any chance of working, Access MUST be run as an administrator i.e. with elevated privileges.
Did you do that?

I have an example app which writes to the HKLM hive and therefore also requires Access to be run as an administrator.
JET ShowPlan Manager - Mendip Data Systems (isladogs.co.uk)

Its not the latest version of the app but the registry part of the code is identical.

EDIT:
One quick point. In your code in e.g. post #19, should there be a backslash before \light
 
Last edited:
I understand the principle of adding obscure registry keys that would be difficult for anyone to detect / alter.
Even so, I would still advise against using HKCR hive. HKLM would be preferable in my opinion

And despite asking more than once I'm still not clear whether you have tried running the code whilst running Access as an administrator.
If not, nothing will happen.

BTW - do use a different registry key value in the real db just in case one of your end users ever reads this thread ;)
 
Last edited:
I understand the principle of adding obscure registry keys that would be difficult for anyone to detect / alter.
Even so, I would still advise against using HKCR hive. HKLM would be preferable in my opinion

And despite asking more than once I'm still not clear whether you have tried running the code whilst running Access as an administrator.
If not, nothing will happen.

BTW - do use a different registry key value in the ral db just in case one of your end users ever reads this thread ;)
BTW - do use a different registry key value in the ral db just in case one of your end users ever reads this thread : OK @isladogs ;) :ROFLMAO:

I will ask UncleThe_Doc_Man to delete the topic permanently hahaha
 

Users who are viewing this thread

Back
Top Bottom